在Javascript中遍历一系列日期
Iterate through a range of dates in Javascript
在过去的几天里,我一直在努力迭代一系列日期。我使用以下代码进行测试:
var current_date = new Date("2014-08-01");
var end_date = new Date("2014-10-31");
var end_date_time = end_date.getTime();
while (current_date.getTime() <= end_date_time) {
document.write(current_date + '<br>');
current_date.setDate(current_date.getDate()+1);
}
对我来说,这看起来是正确的,但有一个问题。它错过了最后一天。我扭转了这段代码,使用了for循环,在循环中定义了新的Date,以及所有你能想象到的事情。有一件事保持不变。最后一天不见了!
出于好奇,我使用了以下格式创建日期:
var current_date = new Date("08/01/2014");
var end_date = new Date("10/31/2014");
令我惊讶的是,它不出所料地发挥了作用。现在我想知道这是一种正常的行为还是Date中的错误?
如果有人能启发我,我将不胜感激。
这是因为时间发生了变化,请检查第一天是否在GMT夏令时,最后一天是否在(GMT标准时间)
所以你的代码最好使用UTC
var current_date = new Date("2014-08-01");
current_date = new Date(current_date.getUTCFullYear(), current_date.getUTCMonth(), current_date.getUTCDate(), current_date.getUTCHours(), current_date.getUTCMinutes(), current_date.getUTCSeconds());
var end_date = new Date("2014-10-31");
end_date = new Date(end_date.getUTCFullYear(), end_date.getUTCMonth(), end_date.getUTCDate(), end_date.getUTCHours(), end_date.getUTCMinutes(), end_date.getUTCSeconds());
var end_date_time = end_date.getTime();
while (current_date <= end_date) {
document.write(current_date + '<br>');
current_date.setDate(current_date.getDate()+1);
}
相关文章:
- 比较从函数和生成的日期对象
- Json数据包含日期和时间格式
- Javascript日期格式类似于ISO,但本地
- 如何在输入字段中的按钮的帮助下打开日历,该字段的类型为“=”;日期”;
- 如何在JavaScript中延长当前日期
- 两位数的月份日期验证
- 更改angularjs中的日期-时间格式
- 将日期时间作为 JSON 发送将无法在我的视图中正确显示
- 将日期时间从json转换为可读格式
- NodeJS日期格式不起作用
- jquerydatetimepicker日期和时间应在框默认值中,而无需单击框
- 使用当前日期生成随机id
- primefaces日历可以禁用过去的日期和时间吗
- 提前阅读日期
- 有没有一个日历可以在谷歌关闭库中选择一系列日期
- 在javascript中计算一系列日期之间的重复日期
- 在Javascript中遍历一系列日期
- 将今天的日期与一系列日期进行比较
- 什么'这是允许用户在html/javascript中指定一系列日期/时间戳的最干净的方法
- 是否存在在特定日期取消对一系列单元格的保护的谷歌应用程序脚本