当前时间与倒计时
2022年4月13日
当前时间与春节倒计时
<span></span>
function getTime() {
var weekday = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
var time = new Date();
var year = time.getFullYear();
var month = time.getMonth() + 1;
var date = time.getDate();
var week = weekday[time.getDay()];
var hours = time.getHours();
var minutes = time.getMinutes();
if (minutes.toString().length < 2) { minutes = "0" + minutes };
var seconds = time.getSeconds();
if (seconds.toString().length < 2) { seconds = "0" + seconds };
return (`${year}年${month}月${date}日 ${week} ${hours} : ${minutes} : ${seconds}`);
};
function FreshTime() {
var nowtime = new Date();
var endtime = new Date("2023/1/22,00:00:00");
var lefttime = parseInt((endtime.getTime() - nowtime.getTime()) / 1000);
var d = parseInt(lefttime / (24 * 60 * 60));
var h = parseInt(lefttime / (60 * 60) % 24);
var m = parseInt(lefttime / 60 % 60);
if (m.toString().length < 2) { m = "0" + m };
var s = parseInt(lefttime % 60);
if (s.toString().length < 2) { s = "0" + s };
if (lefttime <= 0) {
return " 新春快乐!"
} else {
return (` 距春节还有:${d} 天 ${h} 小时 ${m} 分 ${s} 秒 `)
}
};
setInterval(() => {
document.querySelector("span").innerHTML = getTime() + FreshTime();
}, 1000);