asp.net中客户端保持与服务器时间一致
代码是我从一个网站看到的代码改过来的。
后台代码中将服务器时间显示出来
前台使用javascript进行转换
后台代码中将服务器时间显示出来
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"time", "<script type=\"text/javascript\">\r\n\tvar strTime=\"" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\";\r\n</script>\r\n");
前台使用javascript进行转换
strTime=strTime.replace(/(-)/g,"/");
var datNow=new Date(strTime);
var datLocalNow=new Date();
datC=datLocalNow-datNow;
function GetServerTime()
{
var datNow=new Date(new Date()-datC);
var strYear=datNow.getFullYear();
var strMonth=datNow.getMonth()+1;
strMonth=strMonth<10?"0"+strMonth:strMonth;
var strDay=datNow.getDate();
strDay=strDay<10?"0"+strDay:strDay;
var strHour=datNow.getHours();
strHour=strHour<10?"0"+strHour:strHour;
var strMinite=datNow.getMinutes();
strMinite=strMinite<10?"0"+strMinite:strMinite;
var strSecond=datNow.getSeconds();
strSecond=strSecond<10?"0"+strSecond:strSecond;
$("time").innerHTML=strYear+"年"+strMonth+"月"+strDay+"日 "+strHour+":"+strMinite+":"+strSecond
}
GetServerTime();
setInterval("GetServerTime()",1000);
var datNow=new Date(strTime);
var datLocalNow=new Date();
datC=datLocalNow-datNow;
function GetServerTime()
{
var datNow=new Date(new Date()-datC);
var strYear=datNow.getFullYear();
var strMonth=datNow.getMonth()+1;
strMonth=strMonth<10?"0"+strMonth:strMonth;
var strDay=datNow.getDate();
strDay=strDay<10?"0"+strDay:strDay;
var strHour=datNow.getHours();
strHour=strHour<10?"0"+strHour:strHour;
var strMinite=datNow.getMinutes();
strMinite=strMinite<10?"0"+strMinite:strMinite;
var strSecond=datNow.getSeconds();
strSecond=strSecond<10?"0"+strSecond:strSecond;
$("time").innerHTML=strYear+"年"+strMonth+"月"+strDay+"日 "+strHour+":"+strMinite+":"+strSecond
}
GetServerTime();
setInterval("GetServerTime()",1000);
基于JQ的插件封装
JQ