JS倒计时是任何一个月的31号
JS countdown breaks the 31st of any month
我一直在使用我在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);
- 创建一个数组数组,没有任何重复的Javascript
- 任何一种简单的方法都可以将带有onload的元素作为目标
- 在scala或scalajs Diode中,现有类型中的任何一种都符合“;更新一个没有'还不存在”;
- 有没有一种方法可以使用任何浏览器扩展整个dom树'的开发工具
- 有没有一种优雅的方式来告诉esint,以确保我们're没有使用任何ES6语法/函数
- 如何在jQuery中一次写入设置多个类号
- 数据表自动生成的序列号无法正常工作.对于每一页
- 如果发现任何数据,如何停止每7秒运行一次的函数
- 可以't在对另一个数组排序后访问任何数组
- Regex匹配除以7开头的10位数字以外的任何一组数字
- 我想在客户端单击中生成作业序列号.如果有任何建议,请提供帮助
- 是否有一种本机方法可以将字符串从任何区域设置转换为数字
- 如何匹配并返回一个字符串的多个实例,其中单个撇号可以包含在任何索引中
- r规则,用于每月在 31 日或最近一天重复
- 一种脚本,它从数组中随机加载图像,在查看完所有图像之前不重复任何图像
- Chained Promise with Express:在最后一步中未收到任何数据
- 从引导框架发送电子邮件 我的错误是我收到一条消息是邮件已成功发送,但我没有收到任何邮件
- JavaScript,如何检查字符是否为以下任何一种
- Javascript检查字符串是否在两个数组中的任何一个数组中
- JS倒计时是任何一个月的31号