Javascript JS倒计时从任何月份的31号开始

Javascript JS倒计时从任何月份的31号开始,javascript,Javascript,我一直在使用我在interwebz上找到的javascript来计算特定日期/时间的剩余时间,包括它的时区。到目前为止,这个脚本一直运行良好(非常感谢这个社区) 由于我一直在倒计时到2015年11月10日,脚本一直在正确运行,直到今天,不知何故,它无缘无故地增加了整整几个月的天数。但是如果我将月份改为12月或1月,脚本就可以正常工作 从今天起,10月31日,剧本上说距离11月10日还有40天,我真的不明白为什么?正如我前面提到的,直到今天,剧本一直运作良好 有没有人能解释它为什么会这样,以及如何

我一直在使用我在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>

//****时区倒计时Javascript****//
/*
参观http://rainbow.arch.scriptmania.com/scripts/
对于这个脚本和更多
*/
//////////在此配置倒计时脚本//////////////////
变量月份='11';/'*'对于下个月,本月为“0”,或本月为1到12
变量日='10';//月日或+日的抵销
var小时=14;//一天中的小时数为0到23
var tz=-5;//从UTC到时区的偏移量(小时)
var lab='tzcd';//显示时区倒计时的页面条目的id
函数start(){displayTZCountDown(setTZCountDown(月、日、小时、日)、实验室);}
//**如果需要,可以更改启动功能**
window.onload=开始;
//////////请勿编辑超过此行的内容//////////////////
功能设置tz倒计时(月、日、小时、tz)
{
var toDate=新日期();
如果(month=='*')toDate.setMonth(toDate.getMonth()+1);
如果(月份>0),则执行其他操作
{ 

如果(月日期计算代码中有错误,这意味着在该月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日,但该日期不存在。JavaScript日期对象通过将月末的无效日期“滚动”到下一个月来处理这些日期。因此,在此行之后,
toDate
的值为2015年12月1日

toDate.setDay(day);
最后,
toDate
将于2015年12月10日结束。这是自2015年10月31日起的40天

toDate.setMonth(month-1);
与其单独调用
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 toDate=新日期();
如果(month=='*')toDate.setMonth(toDate.getMonth()+1);
如果(月份>0),则执行其他操作
{ 
如果(第0个月){

如果(月)类似的问题:你不仅解决了我的问题,而且让我完全理解了为什么这个错误首先发生。卢克·伍德沃德,我向你致敬。