JS倒计时是任何一个月的31号

JS countdown breaks the 31st of any month

本文关键字:31号 任何一 倒计时 JS      更新时间:2023-09-26

我一直在使用我在interwebz上找到的一个javascript来计算特定日期/时间的剩余时间,包括它的时区。到目前为止,这个脚本运行得还不错(非常感谢这个社区)。

当我一直在倒计时到2015年11月10日时,剧本一直运行正常,直到今天,它莫名其妙地增加了整整几个月的天数。但如果我把月份改成12月或1月,剧本就可以了。

截至今天,也就是10月31日,剧本说离11月10日还有40天,我真的无法理解为什么?正如我之前提到的,直到今天,剧本一直运行得很好。

有人能解释它为什么会这样,以及如何修复它吗?

更新:当地时间一到00:00(2015年11月1日),倒计时就从39天变成了9天。

更新2:如果我将本地日期更改为12月31日,并倒计时到1月10日,则会出现相同的问题。但是,如果我将当地日期更改为11月30日,并倒计时至12月10日,则不会出现问题。因此,这个问题似乎被孤立到任何一个月的31日。

完整(修改)脚本如下:

<!--Copy and paste just above the close </BODY> of you HTML webpage.-->
<SCRIPT type="text/javascript">
// ****  Time Zone Count Down Javascript  **** //
/*
Visit http://rainbow.arch.scriptmania.com/scripts/
 for this script and many more
*/
////////// CONFIGURE THE COUNTDOWN SCRIPT HERE //////////////////
var month = '11';     //  '*' for next month, '0' for this month or 1 through 12 for the month 
var day = '10';       //  Offset for day of month day or + day  
var hour = 14;        //  0 through 23 for the hours of the day
var tz = -5;         //  Offset for your timezone in hours from UTC
var lab = 'tzcd';    //  The id of the page entry where the timezone countdown is to show
function start() {displayTZCountDown(setTZCountDown(month,day,hour,tz),lab);}
    // **    The start function can be changed if required   **
window.onload = start;
////////// DO NOT EDIT PAST THIS LINE //////////////////
function setTZCountDown(month,day,hour,tz) 
{
var toDate = new Date();
if (month == '*')toDate.setMonth(toDate.getMonth() + 1);
else if (month > 0) 
{ 
if (month <= toDate.getMonth())toDate.setFullYear(toDate.getFullYear() + 1);
toDate.setMonth(month-1);
}
if (day.substr(0,1) == '+') 
{var day1 = parseInt(day.substr(1));
toDate.setDate(toDate.getDate()+day1);
} 
else{toDate.setDate(day);
}
toDate.setHours(hour);
toDate.setMinutes(0-(tz*60));
toDate.setSeconds(0);
var fromDate = new Date();
fromDate.setMinutes(fromDate.getMinutes() + fromDate.getTimezoneOffset());
var diffDate = new Date(0);
diffDate.setMilliseconds(toDate - fromDate);
return Math.floor(diffDate.valueOf()/1000);
}
function displayTZCountDown(countdown,tzcd) 
{
if (countdown < 0) document.getElementById(tzcd).innerHTML = "Sorry, you are too late."; 
else {var secs = countdown % 60; 
if (secs < 10) secs = '0'+secs;
var countdown1 = (countdown - secs) / 60;
var mins = countdown1 % 60; 
if (mins < 10) mins = '0'+mins;
countdown1 = (countdown1 - mins) / 60;
var hours = countdown1 % 24;
var days = (countdown1 - hours) / 24;
document.getElementById(tzcd).innerHTML = days + " day" + (days == 1 ? '' : 's') + ' + ' +hours+ 'h : ' +mins+ 'm : '+secs+'s';
setTimeout('displayTZCountDown('+(countdown-1)+','''+tzcd+''');',999);
}
}
</SCRIPT>
<p><font face="arial" size="-2">The countdown script at </font><br><font face="arial, helvetica" size="-2"><a href="http://rainbow.arch.scriptmania.com/scripts/">Rainbow Arch</a></font></p>

日期计算代码中有一个错误,这意味着它在月31日运行时无法正常工作。

以下是在10月31日运行时倒数到11月10日时运行的代码;我已经删掉了if语句中所有不运行的代码:

var month = '11';
var day = '10';
var toDate = new Date();
toDate.setMonth(month-1);
toDate.setDate(day);

为什么倒计时计时器显示从10月31日到11月10日还有40天?让我们逐步了解上面的代码:

var toDate = new Date();

此时,toDate为2015年10月31日。

toDate.setMonth(month-1);

month-1是10,代表11月。这将把toDate设置为2015年11月31日,但该日期并不存在。JavaScriptDate对象通过将月底后的无效日期"滚动"到下一个月来处理这些日期。因此,在这一行之后,toDate的值为2015年12月1日。

toDate.setDay(day);

最后,toDate于2015年12月10日结束。这是自2015年10月31日起的40天。

与其单独调用setFullYear()setMonth()setDate(),不如将所有值收集在一起,然后一次性从所有这些值创建一个Date对象。这样可以避免日期对象在日期计算过程中具有无效的中间值。

我建议在函数setTZCountDown、中替换以下代码

var toDate = new Date();
if (month == '*')toDate.setMonth(toDate.getMonth() + 1);
else if (month > 0) 
{ 
if (month <= toDate.getMonth())toDate.setFullYear(toDate.getFullYear() + 1);
toDate.setMonth(month-1);
}
if (day.substr(0,1) == '+') 
{var day1 = parseInt(day.substr(1));
toDate.setDate(toDate.getDate()+day1);
} 
else{toDate.setDate(day);
}

具有以下内容:

var now = new Date();
var countdownToYear = now.getFullYear();
var countdownToMonth = now.getMonth();
var countdownToDay = now.getDate();
if (month === '*') {
    countdownToMonth += 1;
} else if (month > 0) { 
    if (month <= now.getMonth()) {
        countdownToYear += 1;
    }
    countdownToMonth = month - 1;
}
if (day.substr(0,1) === '+')  {
    var day1 = parseInt(day.substr(1), 10);
    countdownToDay += day1;
} else {
    countdownToDay = day;
}
var toDate = new Date(countdownToYear, countdownToMonth, countdownToDay);
相关文章: