有没有办法获得 真实的 当前 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/

10-17 00:27