gpt4 book ai didi

javascript - 如何获得真正的 UTC 时间戳(独立于客户端)?

转载 作者:可可西里 更新时间:2023-11-01 01:00:55 25 4
gpt4 key购买 nike

有没有一种方法可以获取真实当前的 UTC 时间戳而不依赖于可能未在每个客户端上正确设置的客户端时间?

我知道使用 JavaScript 的 getTime() 我可以获得以毫秒为单位的 UTC 时间戳。它独立于客户的时区,但我认为它取决于客户的当前时间。

我正在尝试制作一个显示真实 UTC 时间的实时时钟,因此客户的时间是否设置正确并不重要。

最佳答案

获取此 JSON:

http://json-time.appspot.com/time.json

或者

http://www.timeapi.org/utc/now

如果您有自己的服务器,您也可以从中获取它。

以第一个为例(带间隔):

HTML:

<div id="time">Click the button</div>
<button id="gettime">Get the time</button>

JavaScript(使用 JQuery):

$("#gettime").click(function() {
setInterval(ajaxCalltoGetTime, 1000);
});


function ajaxCalltoGetTime() {
$.ajax({
type: "GET",
url: 'http://json-time.appspot.com/time.json',
dataType: 'jsonp'
})
.done(function( msg ) {
$("#time").html(msg.hour + ":" + msg.minute + ":" + msg.second );
});

JSFiddler: http://jsfiddle.net/op8xdsrv/

关于javascript - 如何获得真正的 UTC 时间戳(独立于客户端)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27019104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com