asp.net页面中动态获取服务器时间

获取时间的接口get_system_date:

protected void Page_Load(object sender, EventArgs e)
        {            HttpContext.Current.Response.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            HttpContext.Current.Response.End();
        }

在要显示时间的位置设置一个容器:

<span id="timebox"></span>

ajax获取服务器时间:

function GetServerTrueTime() {
            var nowTime;
            $.ajax({
                url: "/get_system_date.aspx",
                type: "POST",
                datatype: "text",
                async: false,
                success:function(data) {
                    if (data) {
                        nowTime = data;
                    }
                }
            });
            return nowTime;
        }

初始化时间:

        num = 0;
        
        //获取服务器时间
        var servertimestr = GetServerTrueTime();
        var servertime = new Date(servertimestr);

        //获取服务器日期
        var year = servertime.getFullYear();
        var month = servertime.getMonth() + 1;
        var date = servertime.getDate();

        //获取服务器时间
        var hour = servertime.getHours();
        var min = servertime.getMinutes();
        var sec = servertime.getSeconds();

刷新服务器时间的方法:

function Refresh(){
        servertimestr = GetServerTrueTime();
        servertime = new Date(servertimestr);

        year = servertime.getFullYear();
        month = servertime.getMonth() + 1;
        date = servertime.getDate();

        hour = servertime.getHours();
        min = servertime.getMinutes();
        sec = servertime.getSeconds();
}

当时间小于10的时候添加0的方法:

function  addZero(num) {
            num = Math.floor(num);
            return (num <= 9) ? ("0" + num) : num;
        }

格式化输出服务器时间(每60秒去服务器请求一次服务器时间):

        function getServerTime() {
            if (num < 60) {
                num += 1;
                sec++;
                if (sec == 60) {
                    min += 1;
                    sec = 0;
                }
                if (min == 60) {
                    hour += 1;
                    min = 0;
                }
                if (hour == 24) {
                    date += 1;
                    hour = 0;
                }
                if (month == 1 || month == 3 || month == 5 || month == 7 || month == 9 || 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;
                }
            } else {
                num = 0;
                Refresh();
            }
            var ssec = addZero(sec);
            var smin = addZero(min);
            var shour = addZero(hour);
            var sdate = addZero(date);
            var smonth = addZero(month);
            var syear = year;
            $("#timebox").text(syear + "-" + smonth + "-" + sdate + "-" + shour + "-" + smin + "-" + ssec);
            window.setTimeout("getServerTime()", 1000);
        }

demo下载>>


欢迎分享,(木庄网络博客交流QQ群:562366239

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论