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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745466712a4628946.html
评论列表(0条)