实现了目前时间到2018年6月20日十点的倒计时
以及当前时间的打印输出。
<body>
<h2>剩余时间</h2>
<p></p>
<p></p>
<script>
window.οnlοad=function () {
function myTime() {
let newDate=new Date();
let endDate=new Date("2018-6-21 10:00:00");
let leftTime=endDate-newDate;
let d,h,m,s;
if (leftTime>=0){
d=Math.floor(leftTime/1000/60/60/24);
h=Math.floor(leftTime/1000/60/60%24);
m=Math.floor(leftTime/1000/60%60);
s=Math.floor(leftTime/100%600)/10;
}
document.getElementsByTagName("p")[0].innerText=d+"天"+h+"时"+m+"分"+s+"秒";
}
setInterval(myTime,100);
function youTime() {
let newDate=new Date();
let year=newDate.getFullYear();
let month=newDate.getMonth();
let mytime=newDate.getDate();
let myday=newDate.getDay();
let hours=newDate.getHours();
let minutes=newDate.getMinutes();
let seconds=newDate.getSeconds();
let newTIme=year+"年"+month+"月"+mytime+"日";
if (myday==0){
newTIme+="星期日"
}else if (myday==1){
newTIme+="星期一"
}if (myday==2){
newTIme+="星期二"
}if (myday==3){
newTIme+="星期三"
}if (myday==4){
newTIme+="星期四"
}if (myday==5){
newTIme+="星期五"
}if (myday==6){
newTIme+="星期六"
}
newTIme+=hours+"时"+minutes+"分"+seconds+"秒";
if (hours<12){
newTIme+="am";
} else {
newTIme+="pm";
}
document.getElementsByTagName("p")[1].innerText=newTIme;
}
setInterval(youTime,1000);
}
</script>
</body>