javascript - Date.now() returns a value in microseconds and not in milliseconds - Stack Overflow

Date.now(), as per the documentation is supposed to return a Unix timestamp or the Epoch Time, that is,

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 been 1554637694364ms 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
Add a ment  | 

2 Answers 2

Reset to default 6

The 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信