[英]HOW do I creating an array of dates between a start and end date?
I want to create a list of dates starting from 2014/0/1 to 2020/11/31 (dates are represented in JavaScript). 我想创建一个从2014/0/1到2020/11/31的日期列表(日期用JavaScript表示)。
This is the code 这是代码
var initialTime = new Date(2014, 0, 1);
var endTime = new Date( 2050, 11, 31);
var arrTime = [];
arrTime.push(initialTime);
if( initialTime < endTime) {
for( var q = initialTime; q <= endTime; q.setDate(q.getDate() + 1)) {
arrTime.push(q);
}
}
document.querySelector("#Time").innerHTML = arrTime;
This is what the code returns. 这就是代码返回的内容。 It is just a list of " Sun Jan 01 2051 00:00:00 GMT-0500 (EST)."
它只是“ Sun Jan 01 2051 00:00:00 GMT-0500(EST)”的列表。 How do I correct this?
我该如何纠正?
When you do q.setTime( ... )
you are modifying the Date
object itself. 当您执行
q.setTime( ... )
您正在修改Date
对象本身。 You are pushing the same object into the array at each iteration, hence modifying it modifies the entire array. 您在每次迭代时都将同一对象推入数组,因此对其进行修改会修改整个数组。
If you only want the string representations of the dates only, you can do: 如果仅只希望日期的字符串表示形式,则可以执行以下操作:
let initialTime = new Date("2018-03-09Z08:00:00") ,endTime = new Date("2018-03-14Z08:00:00") ,arrTime = [] ; for (let q = initialTime; q <= endTime; q.setDate(q.getDate() + 1)) { arrTime.push(q.toString()); } console.log(arrTime);
Or, if you want to have an array of actual Date
instances: 或者,如果您想要一个实际的
Date
实例数组:
let initialTime = new Date("2018-03-09Z08:00:00") ,endTime = new Date("2018-03-14Z08:00:00") ,arrTime = [] ,dayMillisec = 24 * 60 * 60 * 1000 ; for (let q = initialTime; q <= endTime; q = new Date(q.getTime() + dayMillisec)) { arrTime.push(q); } console.log(arrTime);
You are pushing the same memory reference to the array, hence the changes you make affect all of them. 您正在将相同的内存引用推送到该数组,因此您所做的更改会影响所有这些内存引用。
Try: 尝试:
var copiedDate = new Date(q.getTime());
arrTime.push(copiedDate);
This way you are always pushing a new object. 这样,您总是在推动新对象。
first of all you can not compare two dates using ==
首先,您不能使用
==
比较两个日期
second problem is you need to create a new Date object each time you push one to the array ex. 第二个问题是,每次将一个数组推入数组ex时,都需要创建一个新的Date对象。
.push(new Date(q.getTime())
the next problem is you aren't properly adding a day to the last day each time before you push into the array 下一个问题是,每次插入阵列之前,您都没有在最后一天中正确地添加一天
do something like 做类似的事情
pseudo code --- 伪代码-
var dates = [];
while( firstDate < secondDate ){
// this line modifies the original firstDate reference which you want to make the while loop work
firstDate.setDate(firstDate.getDate() + 1);
// this pushes a new date , if you were to push firstDate then you will keep updating every item in the array
dates.push(new Date(firstDate);
}
var resolution = 1000, // Number of dates to capture between start and end
results = [], // will be populated with the for loop
start = Date.now(), // Set to whatever you want
end = start + (1000 * 60 * 60 * 24), // Set to what ever you want
delta = end - start
for (let i = 0; i < resolution; i++) {
let t = (delta / resolution) * i
results.push(new Date(start + t))
}
console.log(results)
live example: https://jsfiddle.net/5ju7ak75/1/ 实时示例: https : //jsfiddle.net/5ju7ak75/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.