考虑到有时候服务器上的时间与真实的北京时间有点偏差,而有时又会用到这两个方面,所以上网找了找,自己调试了一下,算是个总结吧。
[2007-3-16]
修正:getFullYear()替换getYear()
在以下浏览器可以使用,但未做差别对比:
IE6.0/FireFox1.5.0.5 /Flock0.7.4.1/FireFox2.0
代码如下:
<!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=gb2312" />
<title>JavaScript获取服务器与客户端时间[走动]</title>
<script language="javascript" type="text/javascript">
//因程序执行耗费时间,所以时间并不十分准确,误差大约在2000毫秒以下
var xmlHttp = false;
//获取服务器时间
try {
? xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
? try {
??? xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
? } catch (e2) {
??? xmlHttp = false;
? }
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
? xmlHttp = new XMLHttpRequest();
}
xmlHttp.open("GET", "null.txt", false);
xmlHttp.setRequestHeader("Range", "bytes=-1");
xmlHttp.send(null);
severtime=new Date(xmlHttp.getResponseHeader("Date"));
//获取服务器日期
var year=severtime.getFullYear();
var month=severtime.getMonth()+1;
var date=severtime.getDate();
//获取服务器时间
var hour=severtime.getHours();
var minu=severtime.getMinutes();
var seco=severtime.getSeconds();
//获取客户端时间
localtime=new Date();
//取得时间差
var jtime=Math.abs(localtime.getTime()-severtime.getTime());
var jdate=jtime/(24*60*60*1000);
var jhour=jtime%(24*60*60*1000)/(60*60*1000);
var jminu=jtime%(24*60*60*1000)%(60*60*1000)/(60*1000);
var jsecond=jtime%(24*60*60*1000)%(60*60*1000)%(60*1000)/1000;
//格式化输出客户端时间
function getClientTime(){
localtime=new Date();
var cyear=localtime.getFullYear();
var cmonth=localtime.getMonth()+1;
var cdate=localtime.getDate();
var chour=localtime.getHours();
var cminu=localtime.getMinutes();
var cseco=localtime.getSeconds();
ccyear=addZero(cyear);
ccmonth=addZero(cmonth);
ccdate=addZero(cdate);
cchour=addZero(chour);
ccminu=addZero(cminu);
ccseco=addZero(cseco);
?cinnerdata="当前客户端时间:";
?document.getElementById("clienttime").innerHTML=cinnerdata+ccyear+"-"+ccmonth+"-"+ccdate+" "+cchour+":"+ccminu+":"+ccseco;
?document.getElementById('xctime').innerHTML="时间相差:"+Math.round(jdate)+"天"+Math.round(jhour)+"小时"+Math.round(jminu)+"分钟"+jsecond+"秒 总计:"+jtime+"毫秒";
}
//格式化输出服务器时间
function getSeverTime(){
? seco++;
?if(seco==60){
? minu+=1;
? seco=0;
? }
?if(minu==60){
?? hour+=1;
?? minu=0;
?}
?if(hour==24){
? date+=1;
? hour=0;
?}
//日期处理
if(month==1||month==3||month==5||month==7
||month==8||month==10||month==12)
?{
? if(date==32)
? {
?? date=1;
?? month+=1;
?? }
?}else if(month==4||month==6||month==9||month==11){
? if(date==31){
?? date=1;
?? month+=1;
?? }
?}else if(month==2){
?? if(year%4==0&&year%100!=0){//闰年处理
??? if(date==29){
???? date=1;
???? month+=1;
??? }
?? }else{
??? if(date==28){
???? date=1;
???? month+=1;
??? }
?? }
?}
?if(month==13){
?year+=1;
?month=1;
?}
?sseco=addZero(seco);
?sminu=addZero(minu);
?shour=addZero(hour);
?sdate=addZero(date);
?smonth=addZero(month);
?syear=year;
?innerdata="当前服务器时间:";
?document.getElementById("servertime").innerHTML=innerdata+syear+"-"+smonth+"-"+sdate+" "+shour+":"+sminu+":"+sseco;
?setTimeout("getSeverTime()",1000);
?setTimeout("getClientTime()",100);
}
function addZero(num) {
num=Math.floor(num);
return ((num <= 9) ? ("0" + num) : num);
}
</script>
</head>
<body onload="getSeverTime();">
<p id="servertime"></p>
<p id="clienttime"></p>
<p id="xctime"></p>
</body>
</html>
这篇文章主要参考了:http://www.ice8.cn/article.asp?id=39,可以说思想精髓皆来源于此。
为什么我获取到的还是客户端时间呢?奇怪
非常感谢,找了很久才找到解决方案
Good!!
有没有考虑不同浏览器呢?
比如firefox, 哈哈!
发表评论
相关文章
国内AI资源汇总,AI聊天、AI绘画、AI写作、AI视频、AI设计、AI编程、AI音乐等,国内顺畅访问,无需科学上网。
扫码或点击进入:萤火AI大全
文章分类
最新评论