60

How to show yesterday's date in my textbox the yesterday's date and at the same time, the today's date in ?

I have this home.php where I show the date yesterday(user cannot modify this-readonly) and the date today(user MUST input the date today). And when tomorrow comes and the user visits the home .php s/he will saw the date inputted yesterday, and will input the date again for romorrow's.

E.G: Day1 (march 30, 2011) Yesterday's date: March 29, 2011. (Not editable textbox) Enter date today: (I'll type..) March 30, 2011.

Day 2 (march 31, 2011) Yesterday's date: March 30, 2011. (Not editable textbox) Automatically, this will appear upon hitting the home.php Enter date today: (I'll type..) March 31, 2011.

and so on..

I need a validation that wont accept wrong date format and the format must be: 01-Mar-11 How to do this? :(

2
  • if user can enter only todays date, why is it editable?? why dont you put it there by yourself?? Mar 31, 2011 at 6:18
  • Hi Furgan, I have no prob with entering today's date. My prob is that I can't get the yesterday's date. :( Mar 31, 2011 at 6:27

7 Answers 7

150

Yesterday's date is simply today's date less one, so:

var d = new Date();
d.setDate(d.getDate() - 1);

If today is 1 April, then it is set to 0 April which is converted to 31 March.

Since you also wanted to do some other stuff, here are some functions to do it:

// Check if d is a valid date
// Must be format year-month name-date
// e.g. 2011-MAR-12 or 2011-March-6
// Capitalisation is not important
function validDate(d) {
  var bits = d.split('-');
  var t = stringToDate(d);
  return t.getFullYear() == bits[0] && 
         t.getDate() == bits[2];
}

// Convert string in format above to a date object
function stringToDate(s) {
  var bits = s.split('-');
  var monthNum = monthNameToNumber(bits[1]);
  return new Date(bits[0], monthNum, bits[2]);
}

// Convert month names like mar or march to 
// number, capitalisation not important
// Month number is calendar month - 1.
var monthNameToNumber = (function() {
  var monthNames = (
     'jan feb mar apr may jun jul aug sep oct nov dec ' +
     'january february march april may june july august ' +
     'september october november december'
     ).split(' ');

  return function(month) {
    var i = monthNames.length;
    month = month.toLowerCase(); 

    while (i--) {
      if (monthNames[i] == month) {
        return i % 12;
      }
    }
  }
}());

// Given a date in above format, return
// previous day as a date object
function getYesterday(d) {
  d = stringToDate(d);
  d.setDate(d.getDate() - 1)
  return d;
}

// Given a date object, format
// per format above
var formatDate = (function() {
  var months = 'jan feb mar apr may jun jul aug sep oct nov dec'.split(' ');
  function addZ(n) {
    return n<10? '0'+n : ''+n;
  }
  return function(d) {
    return d.getFullYear() + '-' + 
           months[d.getMonth()] + '-' + 
           addZ(d.getDate());
  }
}());

function doStuff(d) {

  // Is it format year-month-date?
  if (!validDate(d)) {
    alert(d + ' is not a valid date');
    return;
  } else {
    alert(d + ' is a valid date');
  }
  alert(
    'Date in was: ' + d +
    '\nDay before: ' + formatDate(getYesterday(d))
  );
}


doStuff('2011-feb-08');
// Shows 2011-feb-08 is a valid date
//       Date in was: 2011-feb-08
//       Day before: 2011-feb-07
0
73

One liner:

var yesterday = new Date(Date.now() - 864e5); // 864e5 == 86400000 == 24*60*60*1000
6
  • 1
    Might worth using 8640000 rather than doing the calculation 24*60*60*1000 each time. Dec 23, 2015 at 4:46
  • 2
    It should be 86400000
    – Chris
    Feb 4, 2016 at 14:31
  • @danielcorin you're right. I just wanted the answer to be clear. I'll update the answer. 24*60*60*1000 ==> 86400000 ==> 864e5
    – oriadam
    Feb 23, 2016 at 10:46
  • 3
    Actually, this type of micro optimization gains pretty much nothing in terms of performance and hurts readability, I always use 24*60*60*1000 instead. Oct 6, 2020 at 9:51
  • I'd like to use this to get tomorrow as well. I've added + instead of minus, but it plays funny when it gests to 2020-12-31 - basically it doesn't get tomorrow when at the end of the year but it works for every other date. Any idea why?
    – Nick
    Nov 4, 2020 at 7:36
8

Yesterday's date can be calculated as,

var prev_date = new Date();
prev_date.setDate(prev_date.getDate() - 1);
4

Get yesterday date in javascript

You have to run code and check it output

    var today = new Date();
    var yesterday = new Date(today);
    
    yesterday.setDate(today.getDate() - 1);
    console.log("Original Date : ",yesterday);

    const monthNames = [
  "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
];
    var month = today.getMonth() + 1
    yesterday = yesterday.getDate() + ' ' + monthNames[month] + ' ' + yesterday.getFullYear()
   
    console.log("Modify Date : ",yesterday);

1

If someone need one-line code than this is also correct

const yesterday = new Date(new Date().setDate(new Date().getDate() - 1));

happy coding!

1

Yesterday Date can be calculated as:-

let now = new Date();
var defaultDate = now - 1000 * 60 * 60 * 24 * 1;
defaultDate = new Date(defaultDate);
0

I developed a simple and efficient function to return previous date. The function takes in getTime() value and return the integer that could be passed to create a new date object. Below is the code:

function getPrevDate(val: number): number {
  const aDay = 24 * 60 * 60 * 1000;
  const last = new Date().getTime() - val * aDay;
  return last;
}

const last = getPrevDate(5);

console.log(new Date(last), new Date());

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.