当系统时间错误时,如何在javascript中获取正确的时间? [英] when the time of system is wrong ,how can I get correct time in javascript?

查看:78
本文介绍了当系统时间错误时,如何在javascript中获取正确的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

let currentDate = new Date();

CurrentDate与系统时间相关,因此,如果系统时间错误,currentDate将为

CurrentDate is related to the time of system, so if the time of system is wrong ,currentDate will be wrong.

推荐答案

您可以使用原子钟API,例如世界时钟:

You could use an atomic clock API, such as World Clock:

http://worldclockapi.com/api/json/est/now

{
    "$id":"1", "currentDateTime":"2018-10-26T05:27-04:00", "utcOffset":"-04:00:00",
    "isDayLightSavingsTime":true, "dayOfTheWeek":"Friday",
    "timeZoneName":"Eastern Standard Time", "currentFileTime":131850052212461578,
    "ordinalDate":"2018-299", "serviceResponse":null
}

唯一的缺点是需要进行REST调用。因此,在JavaScript调用与获得响应之间会存在延迟。如果您对此有控制权,也可以在服务器端公开自己的端点。

The only downside to this is that it would require making a REST call. So there would be latency between the JavaScript call and when you get your response. You could also expose your own endpoint on the server side, if you have control over that.

这篇关于当系统时间错误时,如何在javascript中获取正确的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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