如何获取真正的UTC时间戳(客户机独立)? [英] How to get the real UTC timestamp (client independent)?

查看:564
本文介绍了如何获取真正的UTC时间戳(客户机独立)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法获得当前UTC时间戳的真实,而不依赖于客户端的时间可能无法在每个客户端上设置正确?



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



我正在尝试制作一个显示UTC时间的实时时钟,所以如果客户的时间设置正确,否则客户的时间是否正确无关紧要。

解决方案

获取此JSON: / p>

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

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

如果你有一个,你也可以从自己的服务器获取它。 p>

有关第一个(间隔)的示例:



HTML: / p>

 < div id =time>点击按钮< / div> 
< button id =gettime>获取时间< / button>

JAVASCRIPT(With JQuery):

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


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

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


Is there a way to get the real current UTC timestamp without being dependent on the client's time which might not be set correctly on every client?

I know with JavaScript's getTime() I can get the UTC timestamp in milliseconds. It's independent of the client's timezone, but I think it's dependent of the client's current time.

I'm trying to make a live clock that shows the real UTC time, so it shouldn't matter if the client's time is set correctly or not for it to be correct.

解决方案

Get this JSON:

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

Or

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

Also you can get it from your own server if you have one.

For an example of the first one (with interval):

HTML:

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

JAVASCRIPT (With 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/

这篇关于如何获取真正的UTC时间戳(客户机独立)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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