界面加載時,動態加載當前時間
setInterval(function () {
var date = new Date();//獲得當前時間
var yy = date.getFullYear();//年份
var mm = date.getMonth() + 1;//獲得月份
mm = (mm < 10 ? "0" + mm : mm);//月份小於10時,前面加個0(例如9 ->09)天,小時,分鍾,秒同理
var dd = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate());//天
var hours = (date.getHours() < 10 ? "0" + date.getHours() : date.getHours());//小時
var minutes = (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes());//分鍾
var seconds = (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds());//秒
document.getElementById("time").innerHTML = yy + '年' + mm + "月" + dd + "日 " + hours + ':' + minutes + ':' + seconds;
}, 1000);
1000是代表過已秒鍾刷新一次
上面判斷時間前面沒有2位時,加個0是按標准格式來顯示,不判斷也可以根據個人的需求來