使用 moment.js 返回倒计时
returning a negative countdown using moment.js
我创建了一个倒计时,但它 return 输出的是负数而不是正数。我已经研究了发生这种情况的原因,但没有运气。有谁知道为什么倒计时可以 return 为负?我在我的代码中的哪个位置将其变为负值?
提前致谢!
var now = moment();
var targetDay = now.format("2020-11-03", "dddd, MMMM Do YYYY");
var countDown= Math.floor(moment().diff(targetDay, 'seconds'));
var Days, Minutes,Hours,Seconds;
setInterval(function(){
// Updating Days
Days =pad(Math.floor(countDown / 86400),2);
//updating Hours
Hours = pad(Math.floor((countDown - (Days * 86400)) / 3600),2);
// Updating Minutes
Minutes =pad(Math.floor((countDown - (Days * 86400) - (Hours * 3600)) / 60),2);
// Updating Seconds
Seconds = pad(Math.floor((countDown - (Days * 86400) - (Hours* 3600) - (Minutes * 60))), 2);
// Updation our HTML view
document.getElementById("days").innerHTML=Days + ' Days';
document.getElementById("hours").innerHTML=Hours + ' Hours';
document.getElementById("minutes").innerHTML=Minutes+ ' Minutes';
document.getElementById("seconds").innerHTML=Seconds + ' Seconds';
// Decrement
countDown--;
if(countDown === 0){
countDown= Math.floor(moment().diff(targetDay, 'seconds'));
}
},1000);
// Function for padding the seconds i.e limit it only to 2 digits
function pad(num, size) {
var s = num + "";
while (s.length < size) s = "0" + s;
return s;
}
引用自 momentjs 文档:
By default, moment#diff will return a number rounded towards zero
(down for positive, up for negative)
If the moment is earlier than the moment you are passing to
moment.fn.diff, the return value will be negative.
因此,要解决此问题,您应该使用以下代码
var now = moment();
var targetDay = moment('2020-11-23', 'YYYY-MM-DD');
var countDown= Math.floor(targetDay.diff(now, 'seconds'));
值将为正,因为 targetDay 时刻晚于您传递给 moment.diff
的时刻
我创建了一个倒计时,但它 return 输出的是负数而不是正数。我已经研究了发生这种情况的原因,但没有运气。有谁知道为什么倒计时可以 return 为负?我在我的代码中的哪个位置将其变为负值? 提前致谢!
var now = moment();
var targetDay = now.format("2020-11-03", "dddd, MMMM Do YYYY");
var countDown= Math.floor(moment().diff(targetDay, 'seconds'));
var Days, Minutes,Hours,Seconds;
setInterval(function(){
// Updating Days
Days =pad(Math.floor(countDown / 86400),2);
//updating Hours
Hours = pad(Math.floor((countDown - (Days * 86400)) / 3600),2);
// Updating Minutes
Minutes =pad(Math.floor((countDown - (Days * 86400) - (Hours * 3600)) / 60),2);
// Updating Seconds
Seconds = pad(Math.floor((countDown - (Days * 86400) - (Hours* 3600) - (Minutes * 60))), 2);
// Updation our HTML view
document.getElementById("days").innerHTML=Days + ' Days';
document.getElementById("hours").innerHTML=Hours + ' Hours';
document.getElementById("minutes").innerHTML=Minutes+ ' Minutes';
document.getElementById("seconds").innerHTML=Seconds + ' Seconds';
// Decrement
countDown--;
if(countDown === 0){
countDown= Math.floor(moment().diff(targetDay, 'seconds'));
}
},1000);
// Function for padding the seconds i.e limit it only to 2 digits
function pad(num, size) {
var s = num + "";
while (s.length < size) s = "0" + s;
return s;
}
引用自 momentjs 文档:
By default, moment#diff will return a number rounded towards zero (down for positive, up for negative) If the moment is earlier than the moment you are passing to moment.fn.diff, the return value will be negative.
因此,要解决此问题,您应该使用以下代码
var now = moment();
var targetDay = moment('2020-11-23', 'YYYY-MM-DD');
var countDown= Math.floor(targetDay.diff(now, 'seconds'));
值将为正,因为 targetDay 时刻晚于您传递给 moment.diff
的时刻