Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is 275481/69/100089 the max date "new Date()" will parse? [duplicate]

Assuming a 32-bit OS/Browser, could a Date object created in JavaScript rollover to 1970 if I set a date beyond 2038?

The Mozilla documentation says a year can be set to 9999, however I don't know if this is consistent across all JavaScript implementations, or if this is an accurate description of what the specification dictates.

I would think given the wording in the documentation, it seems like it's either using a 64-bit number for storing the time or storing the actual data in ISO date format.

Does anyone know how browsers implement this?

like image 382
Dan Herbert Avatar asked Nov 24 '22 11:11

Dan Herbert


2 Answers

It shouldn't be - according to the ECMAScript specification seciont 15.9.1.1:

Time is measured in ECMAScript in milliseconds since 01 January, 1970 UTC. Leap seconds are ignored. It is assumed that there are exactly 86,400,000 milliseconds per day. ECMAScript number values can represent all integers from –9,007,199,254,740,991 to 9,007,199,254,740,991; this range suffices to measure times to millisecond precision for any instant that is within approximately 285,616 years, either forward or backward, from 01 January, 1970 UTC.

The actual range of times supported by ECMAScript Date objects is slightly smaller: exactly –100,000,000 days to 100,000,000 days measured relative to midnight at the beginning of 01 January, 1970 UTC.

This gives a range of 8,640,000,000,000,000 milliseconds to either side of 01 January, 1970 UTC. The exact moment of midnight at the beginning of 01 January, 1970 UTC is represented by the value +0.

like image 157
Greg Avatar answered Nov 27 '22 02:11

Greg


Only bitwise operators in JS are 32bit. There is no version that changes this, and there is no difference if your OS is 64bit. So if someone is using bitwise on timestamps, this could happen. For example, here I use bitwise or because I want the side-effect of all bitwise operators that they convert to int, just so I loose the milliseconds of the date.

new Date('2038-01-01T01:01:01.345') / 1000 | 0; // 2145913261.
new Date('2039-01-01T01:01:01.345') / 1000 | 0; // -2117518035. Wraps around...

I could be using anything else, such as Math.round, or parseInt and there will be no problem, but if I use bitwise, it's going to wrap around.

like image 43
Miroslav Banov Avatar answered Nov 27 '22 02:11

Miroslav Banov