1 回答
TA贡献5条经验 获得超1个赞
<!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>
添加回答
举报