<!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>获取服务器时间、客户端时间、北京时间</title>
</head>
<script language="javascript" type="text/javascript">
//js获取服务器时间和客户端时间-start
//因程序执行耗费时间,所以时间并不十分准确,误差大约在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);
//需要在服务器相关目录下创建null.txt文件
xmlHttp.setRequestHeader("Range", "bytes=-1");
xmlHttp.send(null);
severtime=new Date(xmlHttp.getResponseHeader("Date"));
//我的是2003的,抓包发现头文件返回是 Last-Modified
//获取服务器日期
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();
var hm=severtime.getMilliseconds();
//获取客户端时间
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>
<body onLoad="getSeverTime();">
<p id="servertime"></p>
<p id="clienttime"></p>
<p id="xctime"></p>
<!--js获取服务器时间和客户端时间 - end-->
<!--获取百度上北京时间 - start-->
<!--时钟显示必须把文件放到服务器上方可显示-->
<h2>北京时间</h2>
<div class="main" id="bjtime">
<div id="bjt" style="margin-top:8px">
<div id="bjt_1">
<div id="swf_clock"></div>
<div id="bjt_info">
<div id="time">加载中...</div>
<div id="date">加载中...</div>
</div>
</div>
</div>
</div>
<script src="http://www.baidu.com/js/aladdin/clock/clock.js"></script>
<script language="javascript">
// JavaScript Document
if (typeof console == "undefined"){
var console = {
log:function(){}
};
}
var swfLoaded = false;
var initTime = undefined;
function setFDF(){
swfLoaded = true;
//console.log(initTime);
if(initTime) setClockTime(initTime);
return true;
}
function setClockTime(time){
if (swfLoaded && swfCtrl) {
//console.log("COLK:"+time);
//try{
swfCtrl.setTime(time);
//}catch(e){}
}
}
(function(){
var week = '日一二三四五六';
var innerHtml = '{0}:{1}:{2}';
var dateHtml = "{0}月{1}日 周{2}";
var timer = 0;
var beijingTimeZone = 8;
function format(str, json){
return str.replace(/{(\d)}/g, function(a, key) {
return json[key];
});
}
function p(s) {
return s < 10 ? '0' + s : s;
}
window.baidu_time = function(timeObj){
var time = timeObj.time;
initTime = time;
if(timer != 0){
clearInterval(timer);
timer = 0;
}
show(time);
timer = setInterval(function(){
time += 1000;
show(time);
}, 1000);
}
function show(time){
var timeOffset = ((-1 * (new Date()).getTimezoneOffset()) - (beijingTimeZone * 60)) * 60000;
var now = new Date(time - timeOffset);
document.getElementById('time').innerHTML = format(innerHtml, [p(now.getHours()), p(now.getMinutes()), p(now.getSeconds())]);
setClockTime(time);
document.getElementById('date').innerHTML = format(dateHtml, [ p((now.getMonth()+1)), p(now.getDate()), week.charAt(now.getDay())]);
}
function init(){
var elm = document.createElement('SCRIPT');
elm.src = 'http://open.baidu.com/app?module=beijingtime&t=' + new Date().getTime();
document.getElementsByTagName('HEAD')[0].appendChild(elm);
setTimeout(function(){init()}, 60000);
}
init();
})();
</script>
<!--获取百度上北京时间 - end -->
</body>
</html>
获取服务器时间、客户端时间、北京时间
最新推荐文章于 2024-06-03 16:29:47 发布