admin管理员组文章数量:1321428
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 + 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?
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 + 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?
Share Improve this question asked Sep 10, 2011 at 23:10 anroestianroesti 11.4k3 gold badges24 silver badges33 bronze badges 2-
You already have the time difference: It's
60000
. – user278064 Commented Sep 10, 2011 at 23:21 - @user278064 have you ever heard the term "providing an example"? – sjngm Commented Sep 10, 2011 at 23:28
4 Answers
Reset to default 4Date
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 pared 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);
本文标签: Timezone Problems when calculating the difference between two Dates in JavaScriptStack Overflow
版权声明:本文标题:Timezone Problems when calculating the difference between two Dates in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742102584a2420868.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论