Can you create dates that are lower than 271800 BC

2020-02-11 02:25发布

This is as low as I can seem to go with javascript date:

var myDate = new Date(0, 0, 1);
myDate.setFullYear("-271800");
alert(myDate);

Anything lower than -271,800 BC throws an invalid date error. Can we go back a million years? Or a billion? Can the date object allow you to describe any date infinitely in the past or future? How might I do something like this?

4条回答
放我归山
2楼-- · 2020-02-11 02:39

You should build your own DateTime for that. It's complexity depends on what you want to achieve...if you want to represent only year, then it is just a simple number...if you want to say what date was the last Sunday in 1 200 000 BC, it is more complex...but have on mind that Sundays didn't exist in that year :)...Gregorian calendar that we use now is introduced 1582 AD, Julian calendar 45 BC (and I'm not sure what was before that). I don't think that even javascript DateTime takes into account that, so setting it to year 271800 BC doesn't make sense.

查看更多
一纸荒年 Trace。
3楼-- · 2020-02-11 02:40

Representing a particular date a million years ago strikes me as meaningless. Julian calendar? Should days of week honor the Babylonian system?

Create your own type for this, decide what you actually need to represent.

--- Updated: This was accepted, so I'll add a few more specific bits. ---

As mentioned in another answer, according to the EcmaScript spec, pg 164 of the fifth edition (link is a .pdf.)

Time is measured in ECMAScript in milliseconds since 01 January, 1970 UTC. In time values 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.

But, this is for theoretical dates. It ignores a few pieces of reality. Days were shorter (by 12 seconds) a million years ago, so some JavaScript math would be inaccurate. Days of the week have been determined with different systems. Months have been defined differently. All to say, decide what you really need to represent.

查看更多
甜甜的少女心
4楼-- · 2020-02-11 02:45

The ECMAScript standard requires the Date object to be able to represent any date and time, to millisecond precision, within 100 million days before or after 1/1/1970. This is a range of plus or minus 273,785 years. In order to represent dates outside of this range you would need to implement your own date object that did not operate on millisecond resolution.

查看更多
▲ chillily
5楼-- · 2020-02-11 03:04

According to https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date:

The JavaScript date is measured in milliseconds since midnight 01 January, 1970 UTC. A day holds 86,400,000 milliseconds. The JavaScript Date object range is -100,000,000 days to 100,000,000 days relative to 01 January, 1970 UTC.

The JavaScript Date object provides uniform behavior across platforms.

Resources are finite and a developer has to make a compromise between storage, performance and range for any given datatype. IMHO the ecmascript range for dates is large enough for any practical matter.

查看更多
登录 后发表回答