最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

datetime - What range of dates are permitted in Javascript? - Stack Overflow

programmeradmin3浏览0评论

What is the maximum and the minimum date that I can use with the Date object in Javascript?

Is it possible to represent ancient historical dates (like January 1, 2,500 B.C.) or dates that are far into the future (like October 7, 10,000)?

If these far-from-present dates can't be represented with the Date object, how should I represent them?

What is the maximum and the minimum date that I can use with the Date object in Javascript?

Is it possible to represent ancient historical dates (like January 1, 2,500 B.C.) or dates that are far into the future (like October 7, 10,000)?

If these far-from-present dates can't be represented with the Date object, how should I represent them?

Share Improve this question asked Sep 30, 2012 at 23:48 Peter OlsonPeter Olson 143k49 gold badges208 silver badges249 bronze badges 3
  • So, what's the point of asking "If these far-from-present dates can't be represented..." when you already knew they could be represented? Why pose the question, if you know it's moot? :P – I Hate Lazy Commented Oct 1, 2012 at 0:10
  • @user1689607 I don't know, the question just felt inplete without it. – Peter Olson Commented Oct 1, 2012 at 0:26
  • Well, it was an interesting question anyway. Good to see JS doesn't suffer the 2038 problem. – I Hate Lazy Commented Oct 1, 2012 at 1:01
Add a ment  | 

1 Answer 1

Reset to default 19

According to §15.9.1.1 of the ECMA-262 specification,

Time is measured in ECMAScript in milliseconds since 01 January, 1970 UTC.
...
The actual range of times supported by ECMAScript Date objects is ... 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.

So the earliest date representable with the Date object is fairly far beyond known human history:

new Date(-8640000000000000).toUTCString()
// Tue, 20 Apr 271,822 B.C. 00:00:00 UTC

The latest date is sufficient to last beyond Y10K and even beyond Y100K, but will need to be reworked a few hundred years before Y276K.

new Date(8640000000000000).toUTCString()
// Sat, 13 Sep 275,760 00:00:00 UTC

Any date outside of this range will return Invalid Date.

new Date(8640000000000001)   // Invalid Date
new Date(-8640000000000001)  // Invalid Date

In short, the JavaScript Date type will be sufficient for measuring time to millisecond precision within approximately 285,616 years before or after January 1, 1970. The dates posted in the question are very fortably inside of this range.

发布评论

评论列表(0)

  1. 暂无评论