主要用到了,getTime()函数,var nowtime = new Date();得到现在时间, var future = new Date("2019/02/05 00:00:00");
得到将来时间,,var timeSum = future.getTime() - nowtime.getTime();得到秒数
实例
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> div { width: 1210px; margin: 200px auto; color: red; text-align: center; font: 600 30px/30px "simsun"; } </style> </head> <body> <div></div> <script> var div=document.getElementsByTagName("div")[0]; var timer = setInterval(fn,1); function fn(){ var nowtime = new Date(); // console.log(nowtime); var future = new Date("2019/02/05 00:00:00"); // console.log(future); var timeSum = future.getTime() - nowtime.getTime(); // console.log(timeSum) var day = parseInt(timeSum/1000/60/60/24); var hour = parseInt(timeSum/1000/60/60%24); var minu = parseInt(timeSum/1000/60%60); var sec = parseInt(timeSum/1000%60); var millsec = parseInt(timeSum%1000); day=day<10?"0"+day:day; hour=hour<10?"0"+hour:hour; minu=minu<10?"0"+minu:minu; sec=sec<10?"0"+sec:sec; if(millsec<10){ millsec="00"+millsec; }else if(millsec<100){ millsec="0"+millsec; } // // console.log(day); // console.log(parseInt(timeSum/1000/60/60/24)); if(timeSum<0){ div.innerHTML="距离春节还有00天00小时00分00秒000毫秒"; clearInterval(timer); return; } div.innerHTML="距离春节还有"+day+"天"+hour+"小时"+minu+"分"+sec+"秒"+millsec+"毫秒"; // console.log(future); } </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例