网页动态显示当前时间

  1. //一般情况显示的是server端当前时间,可能会跟client时间不一样,访问者还是想看到当前client时间,所以用javascript比较方便。
  2. <script language="JavaScript" type="text/javascript">
  3. var timerID = null;
  4. var timerRunning = false;
  5. function stopclock ()
  6. {
  7.       if(timerRunning)
  8.      clearTimeout(timerID);
  9.      timerRunning = false;
  10. }
  11. function startclock ()  
  12. {
  13.      stopclock();
  14.      showtime();
  15. }
  16. function showtime ()
  17. {
  18.      var now = new Date();
  19.     var years = now.getFullYear();
  20.     var months = now.getMonth()+1;
  21.     var dates = now.getDate();
  22.     var hours = now.getHours(); 
  23.     var minutes = now.getMinutes(); 
  24.     var seconds = now.getSeconds();
  25.     var day;
  26.     if(now.getDay()==0)   day   =   "星期日"
  27.     if(now.getDay()==1)   day   =   "星期一"
  28.     if(now.getDay()==2)   day   =   "星期二"
  29.     if(now.getDay()==3)   day   =   "星期三" 
  30.     if(now.getDay()==4)   day   =   "星期四"
  31.     if(now.getDay()==5)   day   =   "星期五"
  32.     if(now.getDay()==6)   day   =   "星期六"
  33.      var timeValue = "" +((hours >= 12) ? "今天下午 " : "今天上午 " )
  34.     timeValue += day + " "
  35.     timeValue += years + "-"
  36.     timeValue += ((months<10) ? "0" : "") + months + "-"
  37.     timeValue += ((dates<10) ? "0" : "") + dates + " "
  38.     timeValue += ((hours >12) ? hours -12 :hours)
  39.     timeValue += ((minutes < 10) ? ":0" : ":") + minutes
  40.     timeValue += ((seconds < 10) ? ":0" : ":") + seconds+" "
  41.     document.clock.thetime.value = timeValue;
  42.     timerID = setTimeout("showtime()",1000);
  43.     timerRunning = true;
  44. }     
  45. </script>
  46. <body οnlοad="startclock()">
  47.     <form id="clock" runat="server">
  48.         <input name="thetime" style="font-size: 9pt; color: #000000; border: 0" size="40" />
  49.     </form>
  50. </body>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值