Is JavaScript's Date object susceptible to the Y2038 problem?

前端 未结 2 1863
谎友^
谎友^ 2021-01-08 01:21

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 se

2条回答
  •  感动是毒
    2021-01-08 02:05

    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.

提交回复
热议问题