简体   繁体   中英

Discrepancy between Chrome/Firefox and IE in javascript Date() calculations

Just noticed that IE puts out a different figure for the value of a date as follows:

new Date("01 Apr 1940 23:59:59").valueOf()
-938826001000

new Date("31 Mar 1900 00:00:01").valueOf()
-2201302799000

Here's the same output from Chrome and Firefox:

new Date("01 Apr 1940 23:59:59").valueOf()
-938826001000
new Date("31 Mar 1900 00:00:01").valueOf()
-2201299199000 

The first number is the same in both cases, but the second number is different in both cases.

When I do a datediff on hours between these two dates with

trunc((a-b)/3600000)

Internet explorer gives the figure that agrees with c# and Vb.net, and chrome/ff are one hour out.

So is this a bug in firefox and chrome, or a bug in IE and c#/vb ?? or am I missing something :)

thanks

Here's some more examples of differences

new Date("01 Apr 1960 23:59:59").valueOf()  // IE
-307674001000

new Date("01 Apr 1960 23:59:59").valueOf()  // CHROME
-307670401000

I tried 1962,1964,1966,1968,1970,1980 they were all the same in both browsers, only 1960 was different.

?new Date("01 Apr 1960 23:59:59").valueOf() + " cr"
"-307670401000 cr" -  DIFFERENT
?new Date("01 Apr 1962 23:59:59").valueOf() + " cr"
"-244602001000 cr"
?new Date("01 Apr 1964 23:59:59").valueOf() + " cr"
"-181443601000 cr"
?new Date("01 Apr 1966 23:59:59").valueOf() + " cr"
"-118371601000 cr"
?new Date("01 Apr 1968 23:59:59").valueOf() + " cr"
"-55213201000 cr"
?new Date("01 Apr 1970 23:59:59").valueOf() + " cr"
"7858799000 cr"

?new Date("01 Apr 1960 23:59:59").valueOf() + " ie"
"-307674001000 ie" -  DIFFERENT
?new Date("01 Apr 1962 23:59:59").valueOf() + " ie"
"-244602001000 ie"
?new Date("01 Apr 1964 23:59:59").valueOf() + " ie"
"-181443601000 ie"
?new Date("01 Apr 1966 23:59:59").valueOf() + " ie"
"-118371601000 ie"
?new Date("01 Apr 1968 23:59:59").valueOf() + " ie"
"-55213201000 ie"
?new Date("01 Apr 1970 23:59:59").valueOf() + " ie"
"7858799000 ie"

You're running into differences in timezone handling. If you supply a fully qualified date with tz offset info:

new Date("1900-03-31T00:00:01.000+00:00").valueOf()

consistency is achieved.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse#Date_Time_String_Format

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM