简体   繁体   English

在JavaScript中使用微秒精度的日期范围

[英]Range using date with microsecond precision in javascript

If I stored a date as a serial number in javascript, for example, the date "2014-01-01" would be stored as 41640 ., "2014-01-01 12:00:00" would be stored as 41640.5 , etc. 例如,如果我将日期作为序列号存储在javascript中,则日期"2014-01-01"将存储为41640 ,日期"2014-01-01 12:00:00"将存储为41640.5等。

If we allowed microsecond precision (and not millisecond precision, like the current Javascript date object has), what would be the largest date that could be stored using the javascript Number? 如果我们允许微秒精度(而不是像当前Javascript date对象那样具有毫秒精度),那么使用javascript Number可以存储的最大日期是多少?

In a day, there are 24*3600*10^6 μs and to store this value of 86400000000=0x141DD76000 accurately, one needs 37 bits. 一天有24 * 3600 * 10 ^ 6μs,要准确存储此值86400000000 = 0x141DD76000,一个需要37位。

There are 52+1 bits in the mantissa. 尾数中有52 + 1位。 So you have 16 bits left to code the day. 因此,您还有16位可用来编码日期。 This allows to code ~2^16/365=179 years and 201 days. 这允许编码〜2 ^ 16/365 = 179年零201天。 As the reference is around 1900, we must also consider the leap years: 24(1900-1999)+20(2000-2079)=48 leap years. 由于参考值大约是1900年,因此我们还必须考虑the年:24(1900-1999)+20(2000-2079)= 48个leap年。

As the reference is precisely december 30th 1899, the coding limit is reached on the dec 30th 2178 + (201-48) days that corresponds to june 2nd 2079. 由于参考的日期恰好是1899年12月30日,因此在20178年12月30日+(201-48)天达到了编码限制,该天数对应于2079年6月2日。

After that, coding will continue with a loss of accuracy and will not precise at the microsecond. 在那之后,编码将继续以准确性为代价,并且将不会以微秒为准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM