admin管理员组文章数量:1290102
I want to create a list of dates starting from 2014/0/1 to 2020/11/31 (dates are represented in 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)." How do I correct this?
I want to create a list of dates starting from 2014/0/1 to 2020/11/31 (dates are represented in 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)." How do I correct this?
Share Improve this question edited Jul 12, 2017 at 21:35 Dalorzo 20k7 gold badges57 silver badges102 bronze badges asked Jul 12, 2017 at 21:27 Waltham WECANWaltham WECAN 4914 gold badges13 silver badges27 bronze badges 2- You are pushing the same object reference to array each and every time. – charlietfl Commented Jul 12, 2017 at 21:30
- Maybe thats duplicate: stackoverflow./questions/4413590/… – Sebastian A. Steins Commented Jul 12, 2017 at 21:31
5 Answers
Reset to default 3When you do q.setTime( ... )
you are modifying the Date
object itself. 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:
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);
first of all you can not pare two dates using ==
second problem is you need to create a new Date object each time you push one to the array ex. .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);
}
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.
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/5ju7ak75/1/
Short ES6 solution:
const getDatesInRange = (min, max) => Array((max-min)/86400000).fill(0).map((_, i) => new Date((new Date()).setDate(min.getDate() + i)))
Example
getDatesInRange(new Date('12-25-2000'), new Date('12-25-2001'))
本文标签: javascriptHOW do I creating an array of dates between a start and end dateStack Overflow
版权声明:本文标题:javascript - HOW do I creating an array of dates between a start and end date? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741465884a2380310.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论