首页 > 编程 > JS > 正文

JS获取服务器时间代码

2020-07-03 13:05:31
字体:
来源:转载
供稿:网友
这篇文章主要为大家详细介绍了JS获取服务器时间代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,有需要的朋友可以收藏方便以后借鉴。

昨天给一个用户访了一个金融站点,在站点页面的右上角有一个获取服务器时间的JS特效代码,361源码觉得还不错,就拿出来分享给大家:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Javascript获取服务器时间代码实例-361源码-www.cuoxin.com</title></head><body><p id="time"></p><script>ajax()  function ajax(option){    var xhr = null;    if(window.XMLHttpRequest){      xhr = new window.XMLHttpRequest();    }else{ // ie      xhr = new ActiveObject("Microsoft")    }    // 通过get的方式请求当前文件(361源码 www.cuoxin.com)    xhr.open("get","/");    xhr.send(null);    // 监听请求状态变化(361源码)    xhr.onreadystatechange = function(){      var time = null,          curDate = null;      if(xhr.readyState===2){        // 获取响应头里的时间戳(www.cuoxin.com)        time = xhr.getResponseHeader("Date");        console.log(xhr.getAllResponseHeaders())        curDate = new Date(time);        document.getElementById("time").innerHTML = "361源码(www.cuoxin.com)提示您服务器时间是:"+curDate.getFullYear()+"-"+(curDate.getMonth()+1)+"-"+curDate.getDate()+" "+curDate.getHours()+":"+curDate.getMinutes()+":"+curDate.getSeconds();      }    }  }</script></body></html>

直接复制代码到网站页面,投放到站点空间测试就能使用。需要注意的一点是:本地测试无效,需要上传到站点

以上就是JS获取服务器时间代码的全部内容,希望对大家的学习和解决疑问有所帮助,也希望大家多多支持错新网。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表