HTML实时获取当前时间
<script type="text/javascript">
function gettime() {
var time=new Date();
var arr=new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
var hour=time.getHours();
var min=time.getMinutes();
var sec=time.getSeconds();
var madetime=time.getFullYear()+"-"+(time.getMonth()+1)+"-"+time.getDate()+" "+arr[time.getDay()]+" "+hour+(min<10?":0":":")+min+(sec<10?":0":":")+sec+(hour<12?" am":" pm");
document.getElementById("nowtime").innerText = madetime;
}
gettime();//显示第一秒的时间
setInterval("gettime()",1000);
</script>
</span>当前时间:<span id="nowtime">时间获取中:)...</span>
效果:
注意:getMonth()返回值是0-11,所以要加1
版权声明:本文为qq_21469731原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。