浏览器中Date.now()的准确性

编程入门 行业动态 更新时间:2024-10-27 03:26:21
本文介绍了浏览器中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()的准确性

本文发布于:2023-10-24 02:56:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1522737.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:器中   准确性   Date

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!