2

亲爱的,我正在使用此函数获取 GMT 时间,当我调用函数 getTime 时,我是 java 脚本新手' by 'document.write' 但它在窗口上没有打印任何内容,请提供任何建议:

这是代码:

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

1 回答 1

1

好吧,您正在做示例,但还没有阅读完整的说明。

在您的情况下,由于您要求它这样做,因此只提醒时间是正常的。如果你想要一个特定的 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;
    });
于 2012-03-22T08:09:42.187 回答