Home > front end >  Why new Date("1651214829629") show Invalid Date in chrome?
Why new Date("1651214829629") show Invalid Date in chrome?

Time:05-27

In chrome i type it will show this? i can under why?

enter image description here

>new Date("1651214829629")

Invalid Date

>new Date(1651214829629)

Fri Apr 29 2022 14:47:09 GMT 0800 (中国标准时间)

>new Date("110")

Wed Jan 01 0110 00:00:00 GMT 0805 (中国标准时间)

CodePudding user response:

Like Hao-Jung Hsieh said, if you give a string to new Date(), it will interpret it as a dateString, not a timestamp. To parse a dateString, new Date() will call Date.parse(), which is detailed in the ECMAScript docs here. Date.parse() will first time to parse the dateString according to the Date Time String Format. The Date Time String Format says that if you only give one number, that number will be interpreted as the year, which is usally in the range 0-9999 (as defined by ISO 8601), but ECMAScript provides an extension to support years outside this range, which is detailed in the Expanded Years section. With this extension, the year can be be in the range -273,790 to 273,790.

So, when you provide run new Date("1651214829629"), Chrome will interpret 1651214829629 as a year. 1651214829629 is not in the acceptable range, therefore it is an invalid date.

CodePudding user response:

when you use a string, new Date() will think it's a dateString instead of a timestamp. "110" is meaning at 1:10. It just can't understand what is the dateString of "1651214829629".

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

  • Related