查看: 439|回复: 0

[JS代码] JS显示当前时间,实时更新

[复制链接]
xuanxiao 发表于 2022-5-29 15:18:51 | 显示全部楼层 |阅读模式
html代码:

  1. <span class="header_span2" id="Servertime" style="float: left; width: 132px; font-size: 12px; text-align: center; display: inline-block; padding-top: 2px; margin-top: 0px"></span>
复制代码

  1. //当前时间
  2. function curentTime() {
  3.                 var now = new Date();
  4.                 var year = now.getFullYear(); //年
  5.                 var month = now.getMonth() + 1; //月
  6.                 var day = now.getDate(); //日
  7.                 var hh = now.getHours(); //时
  8.                 var mm = now.getMinutes(); //分
  9.                 var ss = now.getSeconds();
  10.                 var clock = year + "-";
  11.        
  12.                 if (month < 10)
  13.                         clock += "0";
  14.                 clock += month + "-";
  15.                 if (day < 10)
  16.                         clock += "0";
  17.                 clock += day + " ";
  18.                 if (hh < 10)
  19.                         clock += "0";
  20.        
  21.                 clock += hh + ":";
  22.                 if (mm < 10)
  23.                         clock += '0';
  24.                 clock += mm + ":";
  25.        
  26.                 if (ss < 10)
  27.                         clock += '0';
  28.                 clock += ss;
  29.        
  30.                 $('#Servertime').text(clock);
  31.         }
  32.         curentTime();
  33.         setInterval('curentTime()', 1000);
复制代码


效果如图:
GIF 2022-05-29 15-18-11.gif
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表

在线客服

售前咨询
售后咨询
服务热线
023-58418553
微信公众号