Javascript Date "Invalid Date" in IE when copying very old dates -


code:

var x = new date(date.utc(0, 0, 0)); x.setutcfullyear(0);  // in firefox, writes "date {sat dec 30 0000 16:00:00 gmt-0800 (pacific standard time)}" // in ie, writes "log: sat dec 30 16:00:00 pst 1 b.c." console.log(x);    // create copy of x var y = new date(x);  // in firefox, writes "date {sat dec 30 0000 16:00:00 gmt-0800 (pacific standard time)}" // in ie, writes "log: invalid date"  console.log(y); 

this seems happen old dates

my question(s): invalid here, , why ie? how can move past problem , create copy of date?

it seems when date object passed date constructor in ie, it's evaluated other time value (probably calls tostring).

to force evaluate time value, can do:

new date(x.gettime()); 

or

new date(+x); 

or expression makes date return time value rather string.

when single value passed date constructor, it's converted primitive. specification doesn't whether should converted string or number. ie isn't non–compliant, it's behaving differently.

it unusual though ie doesn't seem correctly parse it's own string representation of date in case. seems fail date before 70-01-01, may moot since gregorian calendar introduced in 1582. time value can cover dates 283458 bc 287396 ad.

anyway, fix simple.

edit 2016

in es5, passing date date constructor called date.prototype.tostring constructor had parse it's own string version of date. ecmascript 2015 fixed time value used directly instead.

however, not browsers support ecmascript 2015 yet though chance of new date(date) returning incorrect value small , becoming smaller day, it's still safer use +date (until ie 8 gone).


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -