32

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?

4
  • Reverse this question: stackoverflow.com/questions/4868241/javascript-date-1 Commented May 6, 2013 at 15:17
  • The solution posted in that thread is exactly the same as the ones I've linked to... Commented May 6, 2013 at 15:35
  • Read comments: date.setTime(date.getTime() + 86400000); - venkatagiri Commented May 6, 2013 at 15:36
  • No need, Pointy's answer is exactly what I was looking for. Commented May 6, 2013 at 15:38

4 Answers 4

63

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.

Sign up to request clarification or add additional context in comments.

5 Comments

That's pretty goofy, if you ask me. They should have called it setDay(), not setDate().
@RobertHarvey: The sheer volume of things they should have done differently with the JavaScript Date object would (and probably does) fill volumes...
@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.
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 yes exactly - it makes doing things like building calendar displays really easy.
17

Try this:

var date = new Date('04/28/2013 00:00:00');
var yesterday = new Date(date.getTime() - 24*60*60*1000);

2 Comments

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: don't forget about leap seconds!
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

Comments

-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]);

1 Comment

Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value 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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.