计算从现在到今年五一放假还有多少天(保留三位)?或者一共多少小时(保留三位小数)?

慕标8001392
浏览 1161回答 1
1回答

sherryliu

<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Document</title></head><body><script>// 现在的日期var nowadays=new Date(),    nowTime=nowadays.getTime();// 五月一日零点var future=new Date();future.setFullYear(2018,4,1);future.setHours(0,0,0,0);var futureTime=future.getTime();// 时间差var days=parseFloat((futureTime-nowTime)/1000/60/60/24);var hours=parseFloat((futureTime-nowTime)/1000/60/60);// 保留三位小数function decimal(num){var fnum=num-parseInt(num);var inum=Math.round(fnum*1000);var integer=parseInt(num)+inum/1000;return integer;}var finalDays=decimal(days);var finalHours=decimal(hours);// 打印结果,注意四舍五入console.log(finalDays);console.log(finalHours);console.log(days);console.log(hours);</script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript