gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:59:10 34 4
gpt4 key购买 nike

亲爱的,我正在使用这个函数来获取 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/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com