获取系统时间和本地时间

来源:互联网 发布:linux grub引导win7 编辑:程序博客网 时间:2024/04/23 23:25

有些时候我们前端需要对时间做一些限制,比如倒计时啊,还有一些其他的抢购类似的。。这些如果获取的是本地时间,其他有心人是可以做一些修改的。所以需要获取系统时间。

当然,这些如果是很重要的东西还是放在后台来做好一点。

<!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=utf-8"/>
<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>


1 0
原创粉丝点击