现在,我可以显示几天,几小时和几秒钟,而不会出现问题。但是我这次想做的是花几天的时间,将它们转换成小时数并显示出来。
例如,如果我将计时器设置为现在的三天,我希望它显示72 hours 20 minutes 30 seconds
。请注意,图片中没有几天,我不想显示3 days
。
我将如何实现这一目标?
var countDownDate = new Date("6 June 2019 15:00:00");
var options = {
hour: 'numeric',
minute: 'numeric',
hour12: true
};
var countDate = countDownDate.toLocaleString('en-US', options);
console.log(countDate);
var now = new Date().getTime();
var timeDifference = countDownDate - now;
var oneDay = 1000 * 60 * 60 * 24;
var days = Math.floor(timeDifference / (oneDay));
var hours = Math.floor((timeDifference % (oneDay)) / (1000 * 60 * 60));
var minutes = Math.floor((timeDifference % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((timeDifference % (1000 * 60)) / 1000);
// Displays the clock counting down
document.querySelector("#timer").innerHTML = `
<span>${hours} </br> hours</span>
<span>${minutes} </br> minutes</span>
<span>${seconds} </br> seconds!</span>`;
if(timeDifference < 0) {
clearInterval(timer);
document.getElementById("timer").innerHTML = "Expired";
}
答案 0 :(得分:1)
每天等于24小时,所以只需将24 *天添加到小时中:)
一个更好的解决方案是像这样计算小时数:
hours = Math.floor(timeDifference / (1000 * 60 * 60));