Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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');
like image 551
matt Avatar asked Apr 20 '26 23:04

matt


1 Answers

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.

like image 198
Jon Skeet Avatar answered Apr 22 '26 11:04

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!