<!doctype html> <html lang="zh-cn"> <head> <meta charset="utf-8" /> <title></title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" type="text/javascript"></script> </head> <body> <script type="text/javascript"> var clientTime = new Date(); var serverTime = { n: clientTime.getFullYear(), y: clientTime.getMonth(), r: clientTime.getDate(), h: clientTime.getHours(), m: clientTime.getMinutes(), s: clientTime.getSeconds(), xq: clientTime.getDay().toString() }; var serverTimeStr = { n: clientTime.getFullYear(), y: clientTime.getMonth(), r: clientTime.getDate(), h: clientTime.getHours().toString(), m: clientTime.getMinutes().toString(), s: clientTime.getSeconds().toString(), xq: clientTime.getDay().toString() }; // var checkFlag = 0;//为了减小误差,可以设置该变量,比如当checkFlag到达600时,重新从服务器取得时间 function formatServerTime() { // checkFlag++; if (serverTime.s < 59) {//60s进制 serverTime.s++; } else { serverTime.s = 0; if (serverTime.m < 59) {//60m进制 serverTime.m++; } else { serverTime.m = 0; if (serverTime.h < 23) {//24h进制 serverTime.h++; } else { serverTime.h = 0; } } } //时 if (serverTime.h < 10) serverTimeStr.h = "0" + serverTime.h.toString(); else serverTimeStr.h = serverTime.h.toString(); //分 if (serverTime.m < 10) serverTimeStr.m = "0" + serverTime.m.toString(); else serverTimeStr.m = serverTime.m.toString(); //秒 if (serverTime.s < 10) serverTimeStr.s = "0" + serverTime.s.toString(); else serverTimeStr.s = serverTime.s.toString(); //月 if (serverTimeStr.y < 10) serverTimeStr.y = "0" + serverTime.y.toString(); else serverTimeStr.y = serverTime.y.toString(); //日 if (serverTime.r < 10) serverTimeStr.r = "0" + serverTime.r.toString(); else serverTimeStr.r = serverTime.r.toString(); //星期 serverTimeStr.xq = serverTime.xq.toString(); $("#divTimes").html(serverTime.n.toString() + "-" + serverTimeStr.y + "-" + serverTimeStr.r + " " + serverTimeStr.h + ":" + serverTimeStr.m + ":" + serverTimeStr.s + " " + serverTimeStr.xq); // if (checkFlag>600){ // checkFlag=0; // getServerTime(); // return; // } // else window.setTimeout("formatServerTime()", 1000); } function getServerTime() { //var myurl = "/MyTimer.aspx?rnd=" + Math.random();//该处为获取服务器时间的地址,返回json数据,此处格式为 [{"h":10,"m":25,"s":9}] $.ajax({ type: "GET", url: "MyTimer.aspx", complete: function (data, textStatus) { serverTime = eval("(" + data.responseText + ")"); formatServerTime(); }, error: function (e) { } }); } $(function () { getServerTime(); }) </script> <div id="divTimes"> loading......</div> </body> </html>
string date = DateTime.Now.ToString("yyyy,MM,dd,HH,mm,ss,dddd"); string[] dateArr = date.Split(','); date = string.Format("{{'n':{0},'y':{1},'r':{2},'h':{3},'m':{4},'s':{5},'xq':'{6}'}}", dateArr[0], dateArr[1], dateArr[2], dateArr[3], dateArr[4], dateArr[5], dateArr[6]); Response.Write(date); Response.End();