现在有很多的方法和插件对时间进行处理,记录学习过程中自己写的获取当前时间的方法
<div class="date">
<span id="yeat-to-data"></span>
<span id="week"></span>
<span id="line">/</span>
<span id="time"></span>
</div>
//获取当前时间
function getNowTime() {
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours()
var second = date.getSeconds()
var minute = date.getMinutes()
var week = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
var weeks = date.getDay()
document.getElementById("yeat-to-data").innerHTML = addZeros(year) + '-' + addZeros(month) + '-' + addZeros(day)
document.getElementById("week").innerHTML = week[weeks]
document.getElementById("time").innerHTML = addZeros(hour) + ':' + addZeros(minute) + ':' + addZeros(second)
}
function addZeros(s) {
return s < 10 ? ('0' + s) : s
}
setInterval(() => { getNowTime() }, 1000);