码迷,mamicode.com
首页 > 编程语言 > 详细

Javascript 客户端实时显示服务器时间

时间:2015-04-01 16:43:08      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

<!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();

  

Javascript 客户端实时显示服务器时间

标签:

原文地址:http://www.cnblogs.com/sallet/p/4383822.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!