js获取时间戳,并同步显示当前时间
<p><span id="year">2020</span>年<span id="mouth">4</span>月<span id="day">22</span>日<span id="hour">15</span>时<span id="min">30</span>分<span id="secd">20</span>秒</p>
<script type="text/javascript">
var data = new Date();
var year = data.getFullYear();
var mouth = data.getMonth()+1;
var day = data.getDay();//星期
var dd = data.getDate();
var hour = data.getHours();
var min = data.getMinutes();
var sec = data.getSeconds();
timer()
function timer(){
setInterval(function(){
if(sec<60){
sec++
}else{
sec = 0;
if (min<60) {
min++
} else{
min = 0;
if (hour<24) {
hour++
} else{
hour=0
}
}
}
document.getElementById("secd").innerHTML = sec;
document.getElementById("year").innerHTML = year;
document.getElementById("mouth").innerHTML = mouth;
document.getElementById("day").innerHTML = dd;
document.getElementById("hour").innerHTML = hour;
document.getElementById("min").innerHTML = min;
document.getElementById("secd").innerHTML = sec;
},1000)
}
</script>