浏览器中Date.now()的准确性 [英] Accuracy of Date.now() in browsers

查看:54
本文介绍了浏览器中Date.now()的准确性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了很多有关如何在JavaScript中获取日期/时间的问题,答案总是像这样

I've seen plenty of questions about how to get the date/time in JavaScript, and the answers always look something like

Date.now()返回以毫秒为单位的UTC时间戳

Date.now() returns the UTC timestamp in milliseconds

但是,这个价值到底有多可靠?它仅基于运行浏览器的任何计算机的系统时钟吗?如果是这样,看起来它可能与真实的UTC时间有很大的不同,但是应该期望多少变化呢?我知道关心精确的毫秒是一个丢失的原因,但是大约几秒钟或几分钟呢?

But how reliable is that value really? Is it just based on the system clock of whatever computer is running the browser? If so, it seems like it could vary arbitrarily far from true UTC time, but how much variation should be expected?. I know caring about accurate milliseconds is a lost cause, but what about on the order of seconds or minutes?

推荐答案

是否仅基于运行浏览器的任何计算机的系统时钟?

Is it just based on the system clock of whatever computer is running the browser?

对.这可能完全不准确.例如,在我的时钟不准确的计算机上,此时 Date.now()返回 1563724931361 ,并将其传递给 new Date ,给出:

Right. It may well be completely inaccurate. For example, on my computer, whose clock is inaccurate, at this moment, Date.now() returns 1563724931361, which, when passed into new Date, gives:

2019年7月21日星期日11:02:11 GMT-0500(中央夏令时)

Sun Jul 21 2019 11:02:11 GMT-0500 (Central Daylight Time)

这是完全错误的.

如果客户希望提供不正确的 Date.now(),那么对于他们来说这样做很简单,尽管对于大多数普通用户而言,通常会很准确,因为大多数人都有准确的时钟.

If the client wishes to present an inaccurate Date.now(), it's pretty trivial for them to do so, though usually, for most ordinary users, it'll be accurate, because most people have accurate clocks.

准确度如何?这取决于硬件,自他们的计算机上次从时间服务器请求时间以来的时间,以及自计算机打开电源以来的时间(通过电源关闭或待机/休眠),但多数情况下,它会赢得胜利.不会超过一分钟.

How accurate? It depends on the hardware, on how long it was since their computer last requested the time from a time server, and on how long since the computer was turned on (either from power off or standby/hibernate), but most times, it won't be off by more than a minute.

这篇关于浏览器中Date.now()的准确性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆