如何通过javascript获取ntp时间 [英] how to get ntp time from through javascript

查看:128
本文介绍了如何通过javascript获取ntp时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Dear all i am using this function to get the GMT time, i am new to java script when i call the function getTime its generating an alert on the webpage as its coded, i want to print to time on the window i try replaced 'alert' by 'document.write' but its printing nothing on the window, any suggestions please:

这里是代码:

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); });


推荐答案

嗯,你正在做的例子,但还没读过整个解释。

Well you are doing and example but haven't read the entire explanation.

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

In your case it is normal to just alert the time since you are asking it to do it. If you want a specific GMT date format you could do:

// 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'并具有以下内容:

If you want to print the time on the window you could add for example a SPAN tag in your HTML with an id let's say 'gmtTime' and have the following:

 // 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获取ntp时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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