前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >js获取服务器时间

js获取服务器时间

作者头像
deepcc
发布2018-05-16 14:18:12
6.5K0
发布2018-05-16 14:18:12
举报
文章被收录于专栏:deepcc
代码语言:javascript
复制
var DD,MM,YY,hh,mm;
if(document.all){
     window.XMLHttpRequest=function(){
         var get=['Microsoft.XMLHTTP','Msxml2.XMLHTTP'];
   for(var i=0;i<get.length;i++){try{return new ActiveXObject(get[i])}catch(e){}};
     };
 }
window.time=new Date();
targetTime=new Date();
function webDate(fn){
  var Htime=new XMLHttpRequest();
  Htime.onreadystatechange=function(){Htime.readyState==4&&(fn(new Date(Htime.getResponseHeader('Date'))))};
  Htime.open('HEAD', '/?_='+(-new Date));
  Htime.send(null);
 }
webDate(function (webTime){
   DD=webTime.getDate(),MM=webTime.getMonth()+1,YY=webTime.getFullYear(),hh=webTime.getHours();mm=webTime.getMinutes();
})

alert(YY+" "+MM+DD+" "+hh+mm)

以下为原方法:

代码语言:javascript
复制
<!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" xml:lang="en" lang="en">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=gbk"/>  
<title>untitled</title>  
<script type="text/javascript">  
    get=function (id){return document.getElementById(id)}  
    if(document.all){  
        window.XMLHttpRequest=function(){  
            var get=['Microsoft.XMLHTTP','Msxml2.XMLHTTP'];  
            for(var i=0;i<get.length;i++){try{return new ActiveXObject(get[i])}catch(e){}};  
        };  
    }  
    webDate=function(fn){  
        var Htime=new XMLHttpRequest();  
        Htime.onreadystatechange=function(){Htime.readyState==4&&(fn(new Date(Htime.getResponseHeader('Date'))))};  
        Htime.open('HEAD', '/?_='+(-new Date));  
        Htime.send(null);  
    }  
    window.time=new Date();  
    targetTime=new Date();  
    time2String=function (t){  
            with(t)return [getFullYear(),'年'  
                ,('0'+(getMonth()+1)).slice(-2),'月'  
                ,('0'+getDate()).slice(-2),'日 '  
                ,('0'+getHours()).slice(-2),': '  
                ,('0'+getMinutes()).slice(-2),': '  
                ,('0'+getSeconds()).slice(-2)].join('')  
    }  
    int2time=function (m){  
        m-=(D=parseInt(m/86400000))*86400000;  
        m-=(H=parseInt(m/3600000))*3600000;  
        S=parseInt((m-=(M=parseInt(m/60000))*60000)/1000);  
        return D+'天'+H+'小时'+M+'分'+S+'秒'  
    }  
    setInterval(function (){  
        webDate(function (webTime){  
            get('web').innerHTML=time2String(time=webTime);  
        })  
        get('locale').innerHTML=time2String(new Date);  
        get('time').innerHTML=int2time(targetTime-time);  
        if ((targetTime-time)<0) {  
            get('time').innerHTML = 'Game Over';  
        }  
    },1000)  
 </script>      
</head>  
<body>  
设定时间:2012年12月31日0时0分0秒<br>  
服务器时间:<span id='web'>loading...</span><br>  
本地时间:<span id="locale">loading...</span><br>  
倒计时时间:<span id="time">loading...</span>  
<script type="text/javascript" charset="utf-8">  
    targetTime=new Date(2012,12,31,00,00,00);  
</script>  
</body>  
</html>  
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2012-09-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档