why javascript's .getTime() + 24*60*60*1000 get's stack after 27 Oct 2013?

matt picture matt · Jun 2, 2013 · Viewed 10.7k times · Source

I was just creating a simple calendar when users clicks next it gets the following day, very simple code:

var dateSelected = new Date('02/06/2013'); //any date 
var day = new Date(dateSelected.getTime() + 24*60*60*1000);
alert(day.getDate());

that works great for all dates but for some reason it doesn't get the next day when the date is 27 Oct 2013

var dateSelected = new Date('10/27/2013');

I don't seem to be able to figure out why, if I go manually to the next day 28 Oct it keeps working fine.

Any ideas why this happens?

UPDATE: I fixed it by adding the time as well as the date:

var dateSelected = new Date('10/27/2013 12:00:00');

Answer

Jon Skeet picture Jon Skeet · Jun 2, 2013

I strongly suspect this is because of your time zone - which we don't know, unfortunately.

On October 27th 2013, many time zones "fall back" an hour - which means the day is effectively 25 hours long. Thus, adding 24 hours to your original value doesn't change day if it started within the first hour of the day.

Fundamentally, you need to work out whether you're actually trying to add a day or add 24 hours - they're not the same thing. You also need to work out which time zone you're interested in. Unfortunately I don't know much about Javascripts date/time API, but this is almost certainly the cause of the problem.