javascript - 如何通过javascript获取ntp时间

标签 javascript ntp

亲爱的,我正在使用这个函数来获取 GMT 时间,当我调用函数 getTime 时,我是 java 脚本的新手,它在网页上生成一个警报作为它的编码,我想在我尝试的窗口上打印到时间用“document.write”替换“alert”,但它在窗口上没有打印任何内容,请提出任何建议:

代码如下:

function getTime(zone, success) {
var url = 'http://json-time.appspot.com/time.json?tz=' + zone,
ud = 'json' + (+new Date());
window[ud]= function(o){
success && success(new Date(o.datetime));
};
document.getElementsByTagName('head')[0].appendChild((function(){
var s = document.createElement('script');
s.type = 'text/javascript';
s.src = url + '&callback=' + ud;
return s;
})());
}
getTime('GMT', function(time){ // This is where you do whatever you want with the time:     
alert(time); });

最佳答案

你正在做例子,但还没有阅读完整的解释。

在你的情况下,因为你要求它这样做,所以只提醒时间是正常的。 如果你想要一个特定的 GMT 日期格式,你可以这样做:

// Get London time, and format it:
getTime('Europe/London', function(time){
var formatted = time.getHours() + ':' 
              + time.getMinutes() + ':'
              + time.getSeconds();
alert( 'The time in London is ' + formatted );
});

编辑:

如果你想在窗口上打印时间,你可以在你的 HTML 中添加一个 SPAN 标签,比如一个 id 让我们说 'gmtTime' 并具有以下内容:

 // Get London time, and format it:
    getTime('Europe/London', function(time){
    var formatted = time.getHours() + ':' 
                  + time.getMinutes() + ':'
                  + time.getSeconds();
    document.getElementById("gmtTime").innerHTML= formatted;
    });

关于javascript - 如何通过javascript获取ntp时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9818411/

相关文章:

javascript - Z-index 和 transition css 属性冲突?

javascript - 使用 javascript 执行 ajax 调用时阻止用户交互

c - Arduino NTP,我想打印数据(DD-MM-YYYY)

python - 使用 NTP 的事件同步

c++ - QT ntp 并消除差异

javascript - jQuery 现场事件在 iPhone 4 上不起作用?

javascript - 检查光标是否在内容可编辑 div 的开头

javascript - 使用 jQuery 返回 XML 文件内容

Java NTP 客户端

linux - ntpdate 总是与一些偏移量进行同步