【发布时间】:2021-03-23 17:50:33
【问题描述】:
我正在尝试在给定的时间范围内创建一个包含所有日期的数组。出于测试目的,我使用的是 2 月 9 日 - 2 月 13 日,因此所需的数组应该是 [Feb 9, Feb 10, Feb 11, Feb 12, Feb 13]。感谢一篇旧文章,我学会了每次都使用 New Date() 创建新的 Date 对象,这样数组就不会像 [Feb 13, Feb 13, Feb 13, Feb 13, Feb 13] 那样结束。但是,它仍然覆盖了我推送到数组的第一个元素;它正在输出 [2 月 10 日、2 月 11 日、2 月 12 日、2 月 13 日、2 月 13 日]。我在 while 循环中包含了一个打印语句,用于在每次推送后跟踪数组,这就是我知道第一个元素是被覆盖的元素的方式。我希望能深入了解为什么会发生这种情况,以及如何获得我想要的阵列?非常感谢任何帮助,谢谢!
这是我正在使用的代码:
var Current_Date = new Date(Date_start) // It starts at Feb 9
Current_Date = new Date(Current_Date.setHours(Current_Date.getHours() - 24)); // Feb 8
var Billing_Dates = []
while (Current_Date.valueOf() <= Date_end.valueOf()) {
Current_Date = new Date(Current_Date.setHours(Current_Date.getHours() + 24));
Billing_Dates.push(Current_Date);
Logger.log(Billing_Dates)
}
}
简化输出(包括每次推送后数组的状态):
[Feb 09]
[Feb 10, Feb 10]
[Feb 10, Feb 11, Feb 11]
[Feb 10, Feb 11, Feb 12, Feb 12]
[Feb 10, Feb 11, Feb 12, Feb 13, Feb 13]
Image of the Exact Output (including the status of the array after every push)
【问题讨论】:
标签: javascript arrays datetime google-apps-script