求js获取服务器时间(动态更新)

解决方案 »

  1.   

     var timerID = null
      var timerRunning = false
      function MakeArray(size) 
      {
      this.length = size;
      for(var i = 1; i <= size; i++)
      {
      this[i] = "";
      }
      return this;
      }
      function stopclock (){
      if(timerRunning)
      clearTimeout(timerID);
      timerRunning = false
      }
      function showtime () {
      var now = new Date();
      var year = now.getYear();
      var month = now.getMonth() + 1;
      var date = now.getDate();
      var hours = now.getHours();
      var minutes = now.getMinutes();
      var seconds = now.getSeconds();
      var day = now.getDay();
      Day = new MakeArray(7);
      Day[0]="星期天";
      Day[1]="星期一";
      Day[2]="星期二";
      Day[3]="星期三";
      Day[4]="星期四";
      Day[5]="星期五";
      Day[6]="星期六";
      var timeValue = "";
      timeValue += year + "年";
      timeValue += ((month < 10) ? "0" : "") + month + "月";
      timeValue += date + "日  ";
      timeValue += (Day[day]) + "  ";
      timeValue += ((hours <= 12) ? hours : hours - 12);
      timeValue += ((minutes < 10) ? ":0" : ":") + minutes;
      timeValue += ((seconds < 10) ? ":0" : ":") + seconds;
      timeValue += (hours < 12) ? "上午" : "下午";
      document.getElementById("datetime").innerText=timeValue;
      timerID = setTimeout("showtime()",1000);
      timerRunning = true
      }
      function startclock () {
      stopclock();
      showtime()
      }
    timevalue就是系统当前时间
      

  2.   


    <mce:script language="JavaScript"><!--  
    //定义一个tick函数,以获取系统的时间  
    function tick()  
    {  
    var year,month,day,hours,minutes,seconds,ap;  
    var intYear,intMonth,intDay,intHours,intMinutes,intSeconds;  
    var today;  
    today=new Date();  
    intYear=today.getYear()+1900;  
    intMonth=today.getMonth()+1;  
    intDay=today.getDate();  
    intHours=today.getHours();  
    intMinutes=today.getMinutes();  
    intSeconds=today.getSeconds();  
    //获取系统时间的小时数  
    if(intHours==0)  
    {  
    hours=intHours+":";  
    ap="凌晨";  
    }  
    else if(intHours<12)  
    {  
    hours=intHours+":";  
    ap="早晨";  
    }  
    else if(intHours==12)  
    {  
    hours=intHours+":";  
    ap="中午";  
    }  
    else  
    {  
    intHours=intHours-12;  
    hours=intHours+":";  
    ap="下午";  
    }  
    //获取系统时间的分数  
    if(intMinutes<10)  
    {  
    minutes="0"+intMinutes+":";  
    }  
    else  
    {  
    minutes=intMinutes+":";  
    }  
    //获取系统时间的秒数  
    if(intSeconds<10)  
    {  
    seconds="0"+intSeconds+" ";  
    }  
    else  
    {  
    seconds=intSeconds+" ";  
    }  
    timeString=intYear+'年'+intMonth+'月'+intDay+'日'+hours+minutes+seconds+ap;  
    document.getElementById("Clock").innerHTML=timeString;  
    //每隔0.1秒钟执行一次tick函数  
    window.setTimeout("tick()",100);  
    }  
    window.onload=tick;  
    //document.getElementById("Clock").innerHTML=timeString;  
    // --></mce:script>  
    body中的div为:<div id="Clock"></div>
      

  3.   


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function showtime() {
      var now = new Date();
      var year = now.getYear(); //年
      var month = now.getMonth() + 1;  //月
      var date = now.getDate();     //日
      var hours = now.getHours();   //小时
      var minutes = now.getMinutes();  //分
      var seconds = now.getSeconds();  //秒
      var day = now.getDay(); 
      Day = new Array(7);
      Day[0]="星期天";
      Day[1]="星期一";
      Day[2]="星期二";
      Day[3]="星期三";
      Day[4]="星期四";
      Day[5]="星期五";
      Day[6]="星期六";
      var timeValue = "";
      timeValue += year + "年";
      timeValue += ((month < 10) ? "0" : "") + month + "月";
      timeValue += date + "日 ";
      timeValue += (Day[day]) + " ";
      timeValue += ((hours <= 12) ? hours : hours - 12);
      timeValue += ((minutes < 10) ? ":0" : ":") + minutes;
      timeValue += ((seconds < 10) ? ":0" : ":") + seconds;
      timeValue += (hours < 12) ? "上午" : "下午";
      document.body.innerHTML = timeValue;
      setTimeout("showtime()",1000);   
      }
      setTimeout("showtime()",1000);
    //-->
    </SCRIPT>
    </HEAD><BODY onLoad="showtime()">
    </BODY>
    </HTML>
      

  4.   

    楼上的这些都是获得客户端的时间吧!!假如客户端的时间一改,那面页面上显示的也就是改之后的时间了楼主看看下面的链接
    http://www.code-123.com/html/2009727175322580.html
      

  5.   

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
        function showtime() {
          var now = new Date();
          var year = now.getYear(); //年
          var month = now.getMonth() + 1;  //月
          var date = now.getDate();     //日
          var hours = now.getHours();   //小时
          var minutes = now.getMinutes();  //分
          var seconds = now.getSeconds();  //秒
          var day = now.getDay(); 
          Day = new Array(7);
          Day[0]="星期天";
          Day[1]="星期一";
          Day[2]="星期二";
          Day[3]="星期三";
          Day[4]="星期四";
          Day[5]="星期五";
          Day[6]="星期六";
          var timeValue = "";
          timeValue += year + "年";
          timeValue += ((month < 10) ? "0" : "") + month + "月";
          timeValue += date + "日 ";
          timeValue += (Day[day]) + " ";
          timeValue += ((hours <= 12) ? hours : hours - 12);
          timeValue += ((minutes < 10) ? ":0" : ":") + minutes;
          timeValue += ((seconds < 10) ? ":0" : ":") + seconds;
          timeValue += (hours < 12) ? "上午" : "下午";
          document.body.innerHTML = timeValue;
          setTimeout("showtime()",1000);      
      }
      setTimeout("showtime()",1000);
    //-->
    </SCRIPT>
    </HEAD><BODY onLoad="showtime()">
    </BODY>
    </HTML>
      

  6.   

    获取服务器时间,就ajax返回服务器时间呗