admin管理员组文章数量:1426483
Date.now()
, as per the documentation is supposed to return a Unix timestamp or the Epoch Time, that is, the number of milliseconds that have lapsed since 1st January 1970.
The current unix timestamp as per this website (or any other valid means of putation) is approximately 1554637184. Note that there are 10 digits in this value. This value is actually in milliseconds.
However, the value returned by Date.now()
in all the three browsers -- Chrome 73, Firefox 66.0.2 and Edge 17.17134 -- is 1554637694364, which is observed to be of 13 digits, and therefore, I infer it is the number of microseconds that have elapsed since 1st January 1970.
What's going on?
I just realized this after years of using it because just now I was debugging some code that made a check like so that failed:
let endTime = ...; // a time later than now expressed as a unix timestamp
let now = Date.now();
if (endTime <= now) {
// And it always came here, and that led me to this discovery
...
}
Date.now()
, as per the documentation is supposed to return a Unix timestamp or the Epoch Time, that is, the number of milliseconds that have lapsed since 1st January 1970.
The current unix timestamp as per this website (or any other valid means of putation) is approximately 1554637184. Note that there are 10 digits in this value. This value is actually in milliseconds.
However, the value returned by Date.now()
in all the three browsers -- Chrome 73, Firefox 66.0.2 and Edge 17.17134 -- is 1554637694364, which is observed to be of 13 digits, and therefore, I infer it is the number of microseconds that have elapsed since 1st January 1970.
What's going on?
I just realized this after years of using it because just now I was debugging some code that made a check like so that failed:
let endTime = ...; // a time later than now expressed as a unix timestamp
let now = Date.now();
if (endTime <= now) {
// And it always came here, and that led me to this discovery
...
}
Share
Improve this question
edited Apr 7, 2019 at 11:52
Water Cooler v2
asked Apr 7, 2019 at 11:48
Water Cooler v2Water Cooler v2
33.9k63 gold badges183 silver badges365 bronze badges
5
- 1 What JavaScript implementation is this? – ChaosPandion Commented Apr 7, 2019 at 11:50
- @ChaosPandion It reports that value on the 3 browsers I am testing on -- Firefox, Chrome and Edge. I have updated the question with the version numbers. – Water Cooler v2 Commented Apr 7, 2019 at 11:53
-
This value is actually in milliseconds.
no, it is seconds, as per any documentation you care to read about unix timestamp ...I infer it is the number of microseconds
you infer incorrectly ... there have been1554637694364ms
since 1970 – Jaromanda X Commented Apr 7, 2019 at 11:53 - Simple manual calculation reveals that 1554637694364ms is about 49 years. Which sounds about right. – mbojko Commented Apr 7, 2019 at 11:54
- 49.26349577800594 years to be precise @mbojko :p – Jaromanda X Commented Apr 7, 2019 at 11:55
2 Answers
Reset to default 6The current epoch time (AKA unix timestamp), 1554637856
is the number of seconds since 01-01-1970, not milliseconds.
Date.now()
returns the epoch time in milliseconds, so you'd want seconds:
if (endTime <= now / 1000) {
...
As of this writing the time in seconds since the UNIX epoch is about 1 554 637 931
. So, the time in milliseconds—the JavaScript time—is about 1 554 637 931 654
.
It’s been about 1.55 gigaseconds since the epoch. Your JavaScript timestamps are, in fact, milliseconds.
本文标签: javascriptDatenow() returns a value in microseconds and not in millisecondsStack Overflow
版权声明:本文标题:javascript - Date.now() returns a value in microseconds and not in milliseconds - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745466712a2659562.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论