实现前台时间与服务器时间同步

  1 <script type="text/javascript">
  2     
  3 
  4     $(function () {
  5          @{ DateTime nowTime = System.DateTime.Now; }
  6                 var currentDate = '@nowTime.ToString("yyyyMMdd  HH:mm:ss")';
  7                 var currentYear = '@nowTime.Year';
  8                 var currentMonth = '@nowTime.Month';
  9                 var currentDay = '@nowTime.Day';
 10                 var currentHour = '@nowTime.Hour';
 11                 var currentMinute = '@nowTime.Minute';
 12                 var currentSecond = '@nowTime.Second';
 13 
 14         var theFirstTime, theSecondTime;
 15 
 16 
 17         var calcTime = function () {
 18             if (parseInt(currentSecond) < 59) {
 19                 currentSecond = parseInt(currentSecond) + 1;
 20             } else {
 21                 currentSecond = "0";
 22                 currentMinute = parseInt(currentMinute) + 1;    //分钟加一,秒钟置零
 23                 if (parseInt(currentMinute) == 60) {
 24                     currentMinute = "0";
 25                     currentHour = parseInt(currentHour) + 1;    //小时加一,分钟置零
 26                     if (parseInt(currentHour) == 24) {
 27                         currentHour = "0";
 28                         currentDay = parseInt(currentDay) + 1;      //日期加一,小时置零
 29                         var tempMonth = parseInt(currentMonth);
 30                         if (parseInt(tempMonth) == 1 || parseInt(tempMonth) == 3 || parseInt(tempMonth) == 5 || parseInt(tempMonth) == 7 || parseInt(tempMonth) == 8 || parseInt(tempMonth) == 10 || parseInt(tempMonth) == 12) {     //大月
 31                             if (parseInt(currentDay) > 31) {
 32                                 currentDay = "1";
 33                                 currentMonth = parseInt(currentMonth) + 1;      //月份加一,日期置零
 34                                 if (parseInt(currentMonth) > 12) {
 35                                     currentMonth = "1";
 36                                     currentYear = parseInt(currentYear) + 1;        //年份加一,月份置零
 37                                 }
 38                             }
 39                         } else {        //小月
 40                             if (parseInt(currentMonth) == 2) { //二月
 41                                 if ((parseInt(currentYear) % 4 == 0 && parseInt(currentYear) % 100 != 0) || (parseInt(currentYear) % 400 == 0)) { //是润年
 42                                     if (parseInt(currentDay) > 29) {
 43                                         currentDay = "1";
 44                                         currentMonth = parseInt(currentMonth) + 1;
 45                                     }
 46                                 } else { //非润年
 47                                     if (parseInt(currentDay) > 28) {
 48                                         currentDay = "1";
 49                                         currentMonth = parseInt(currentMonth) + 1;
 50                                     }
 51                                 }
 52                             } else {    //非二月
 53                                 if (parseInt(currentDay) > 30) {
 54                                     currentDay = "1";
 55                                     currentMonth = parseInt(currentMonth) + 1;
 56                                 }
 57                             }
 58                         }
 59                     }
 60                 }
 61             }
 62         }
 63 
 64         setInterval(function () {
 65             calcTime();
 66             theFirstTime = currentYear + "-" + (parseInt(currentMonth) < 10 ? ("0" + currentMonth.toString()) : currentMonth.toString()) + "-" + (parseInt(currentDay) < 10 ? ("0" + currentDay.toString()) : currentDay.toString());     //年月日   20130530
 67             theSecondTime = (parseInt(currentHour) < 10 ? ("0" + currentHour.toString()) : currentHour.toString()) + ":" + (parseInt(currentMinute) < 10 ? ("0" + currentMinute.toString()) : currentMinute.toString()) + ":" + (parseInt(currentSecond) < 10 ? ("0" + currentSecond.toString()) : currentSecond.toString());    //时分秒   15:17:23
 68             $("#time").html(theFirstTime);
 69             
 70             if (parseInt(theSecondTime.split(':')[0]) > 12 || parseInt(theSecondTime.split(':')[0]) == 12) {
 71                 $("#timem").html("" + theSecondTime + "&nbspPM");
 72             } else {
 73                 $("#timem").html("" + theSecondTime + "&nbspAM");
 74             }
 75                    
 76                     $("#systemTime").val(theFirstTime + " " + theSecondTime);
 77                 }, 1000);
 78 
 79                 //setInterval(function () {
 80                 //    $.ajax({
 81                 //        type: "post",
 82                 //        url: "/Help/GetTime",
 83                 //        cache: false,
 84                 //        data: {},
 85                 //        success: function (output) {
 86                 //            if (output == "" || output == undefined) {
 87                 //                return;
 88                 //            } else {
 89                 //                var value = new Date(parseInt(output.replace("/Date(", "").replace(")/", ""), 10));
 90                 //                value = new Date(parseInt(output.substr(6)));
 91                 //                var value0 = value.format("yyyy-MM-dd");
 92                 //                var value1 = value.format("hh:mm:ss");
 93                 //                $("#systemTime").val(value0 + " " + value1);
 94                 //                theFirstTime = value0;
 95                 //                theSecondTime = value1;
 96                 //                $("#time").html(theFirstTime);
 97                 //                if (parseInt(theSecondTime.split(':')[0]) > 12 || parseInt(theSecondTime.split(':')[0]) == 12) {
 98                 //                    $("#timem").html("" + theSecondTime + "&nbspPM");
 99                 //                } else {
100                 //                    $("#timem").html("" + theSecondTime + "&nbspAM");
101                 //                }
102                 //            }
103                 //        },
104                 //    });
105                 //}, 90000);
106             });
107     </script>
获取服务器时间

 

posted on 2015-04-20 12:56  高达  阅读(434)  评论(0)    收藏  举报

导航