zoukankan      html  css  js  c++  java
  • Javascript 客户端实时显示服务器时间

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

      

  • 相关阅读:
    Visual C# 2008+SQL Server 2005 数据库与网络开发 12.4 自定义控件
    Visual C# 2008+SQL Server 2005 数据库与网络开发 13.1 菜单和菜单事件的创建
    Visual C# 2008+SQL Server 2005 数据库与网络开发 11.6 小结
    vs2005部署报表服务器项目老提示输入用户名密码解决办法
    Excel 文本处理常用函数
    使用APMServ时出现“1、Apache启动失败,请检查相关配置。√MySQL5.1已启动。”错误的终极解决办法:
    <img> 标签的border等属性尽量少用
    时间相减
    全面介绍 Netstat命令的使用技巧!
    apmserver中 mysql的root密码设定
  • 原文地址:https://www.cnblogs.com/sallet/p/4383822.html
Copyright © 2011-2022 走看看