开发者

Timezone Problems when calculating the difference between two Dates in JavaScript

I use the following to calculate the difference between two dates in JavaScript:

var dateOne = new Date; // Now
var dateTwo = new Date( dateOne.getTime() + 60 * 1000 ); // Now + 开发者_StackOverflow中文版One Minute
var difference = new Date( dateTwo - dateOne );

So, logically, difference should be one minute. But Firebug tells me that the difference is one hour off, and the timezone somehow also changes!

 dateOne = Date {Sun Sep 11 2011 01:07:55 GMT+0200 (CET)}
 dateTwo = Date {Sun Sep 11 2011 01:08:55 GMT+0200 (CET)}
 difference = Date {Thu Jan 01 1970 01:01:00 GMT+0100 (CET)}

How can I fix this?


Date is designed for storing exact dates and times, not differences between dates and times. Subtracting those Date objects yields the number of milliseconds between those two dates. You then create a new Date with that number of milliseconds since the Epoch. Since the Epoch is midnight of January 1, 1970, the result will be 12:01 AM of January 1, 1970. Daylight savings time changes the timezone a little.


In JavaScript all times are in GMT. Every time you convert a Date to a string the timezone is "applied" to the output. So you can try to get the difference in milliseconds:

difference = dateTwo.getTime() - dateOne.getTime()

That's obviously 60000 since you added that.

There is no TimeSpan class or something. Dates only store dates.

EDIT:
If you were wondering why there was the output of +2:00 hours and then just +1:00 that's because January 1st is in Standard Time whereas September 11th has Summer Saving Time. It's not because JavaScript does something funny when deducting one date from another.


The previous answers are very good at clarifying the cause of the problem but I think it also makes sense to point out that Javascript has both "native timezone" and "UTC" methods:-

var dtnow = new Date();
var val1 = dtnow.getHours();
var val2 = dtnow.getUTCHours();

...If the machine running that code has a local timezone other than GMT (perhaps UK Daylight Savings which is GMT+01:00), then the return values could be different (depending on the date value you have set and whether the JS engine does or doesn't return GMT from the non-UTC method - i have seen differences, for example in Rhino there IS a difference).

Therefore for the purpose of the calculations you MAY wish to extract all values using the UTC methods, which ensures everything is based upon GMT. That would make the maths "technically correct", but whether that is semantically correct for the given purpose depends on the use case.

For example, 2am in USA compared to 2am in UK, ignoring timezones, is 0. Whereas with timezones it could be 6 (for example). I sometimes struggle with this too and often have to think hard about what I'm trying to achieve before I following one route or another! Always bakes my noodle for a little while :-)


you can calculate a full proof days difference between two dates resting across TZs by the following formula:

var start = new Date('10/3/2015');
            var end = new Date('11/2/2015');
            var days = (end - start) / 1000 / 60 / 60 / 24;
console.log(days);
            // actually its 30 ; but due to daylight savings will show 31.0xxx
            // which you need to offset as below
            days = days - (end.getTimezoneOffset() - start.getTimezoneOffset()) / (60 * 24);
console.log(days);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜