28

These two stack overflow questions ask a similar question, but their solution doesn't seem to work for me: Javascript Yesterday Javascript code for showing yesterday's date and todays date

Given a date, I need the date of the prior day (the day before). Here's a fiddle with the solution suggested above, and a scenario that doesn't work for me: http://jsfiddle.net/s3dHV/

var date = new Date('04/28/2013 00:00:00');
var yesterday = new Date();
yesterday.setDate(date.getDate() - 1);
alert('If today is ' + date + ' then yesterday is ' + yesterday);

For me, that alerts

If today is Sun Apr 28 2013 00:00:00 GMT-0400 (Eastern Daylight Time) then yesterday is Monday May 27 2013 11:12:06 GMT-0400 (Eastern Daylight Time).

Which is obviously incorrect. Why?

Community
  • 1
  • 1
Arbiter
  • 984
  • 1
  • 10
  • 24

4 Answers4

56

You're making a whole new date.

var yesterday = new Date(date.getTime());
yesterday.setDate(date.getDate() - 1);

That'll make you a copy of the first date. When you call setDate(), it just affects the day-of-the-month, not the whole thing. If you start with a copy of the original date, and then set the day of the month back, you'll get the right answer.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • 4
    That's pretty goofy, if you ask me. They should have called it `setDay()`, not `setDate()`. – Robert Harvey May 06 '13 at 15:19
  • 5
    @RobertHarvey: The sheer volume of things they should have done differently with the JavaScript `Date` object would (and probably does) fill volumes... – T.J. Crowder May 06 '13 at 15:19
  • @RobertHarvey yes, or `setDayOfMonth` or whatever. I have a soft spot however because the Date object (while having all sorts of problems) is really handy. – Pointy May 06 '13 at 15:19
  • 3
    But being fair: One of the *right* things is that Pointy's code works even if `date` is the first day of a month, and so `date.getDate() - 1` is `0`. It will give you Feb 28th (or Feb 29th as appropriate) if `date` is March 1st, for instance. – T.J. Crowder May 06 '13 at 15:21
  • @T.J.Crowder yes exactly - it makes doing things like building calendar displays really easy. – Pointy May 06 '13 at 15:22
15

Try this:

var date = new Date('04/28/2013 00:00:00');
var yesterday = new Date(date.getTime() - 24*60*60*1000);
Vadim
  • 8,701
  • 4
  • 43
  • 50
  • 1
    Not all days are 24 hours long. We have daylight savings time in my country which means we have a 23 hour day once a year, and a 25 hour day once a year. Your code won't handle this correctly. – Arbiter Mar 07 '18 at 19:54
  • @Arbiter: don't forget about leap seconds! – izogfif Mar 19 '18 at 10:34
0

Use this simple function :

private _getYesterdayDate(): Date{
  const yesterday:Date = new Date();
  yesterday.setDate(yesterday.getDate() - 1);
  return yesterday;
}

The function works even if date is the first day of a month, and you used date.getDate() - 1, it will give you last day in the previous month as appropriate

Mouad Chaouki
  • 538
  • 4
  • 6
-4
var allmonths = [
    '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'
];
var alldates = [
    '01', '02', '03', '04', '05', '06', '07', '08', '09', '10',
    '11', '12', '13', '14', '15', '16', '17', '18', '19', '20',
    '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'
];

var today = "2014-12-25";   
var aDayBefore = new Date(today);
aDayBefore.setDate(aDayBefore.getDate() - 1);

document.write(aDayBefore.getFullYear() 
  + '-' + allmonths[aDayBefore.getMonth()] 
  + '-' + alldates[aDayBefore.getDate() - 1]);
markus
  • 40,136
  • 23
  • 97
  • 142
Riyanto Wibowo
  • 364
  • 1
  • 6
  • 13
  • 1
    Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation would greatly improve its long-term value](//meta.stackexchange.com/q/114762/206345) by showing _why_ this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Blue Oct 19 '18 at 13:45