Reputation: 3173
How should I iterate between time range and set each day a new data in format YYYY-mm-dd for some filed in the object?
const index = (time) => {
return {index: `time-&{time}`} // YYYY-mm-dd
}
const start = new Date(2018, 03, 28);
const end = new Date(2019, 03, 28);
let loop = new Date(start); // should be let loop here I guess
while(loop <= end){
var newDate = loop.setDate(loop.getDate() + 1);
loop = new Date(newDate).toISOString().substring(0, 10);
index(loop)
}
Upvotes: 1
Views: 1017
Reputation: 7168
Some corrections were needed.
Use ${}
instead of &{}
in string
for variables.
Dates can be compared with date.getTime()
which is a
The getTime() method returns the number of milliseconds between midnight of January 1, 1970 and the specified date.
Just check beginning and end condition.
const index = (time) => {
return {
index: `time-${time}`
} // YYYY-mm-dd
}
const start = new Date(2018, 03, 28);
const endTime = new Date(2018, 04, 28).getTime();
let loop = new Date(start);
while (loop.getTime() <= endTime) {
let nextDate = loop.setDate(loop.getDate() + 1);
loop = new Date(nextDate);
console.log(index(loop.toISOString().substring(0, 10)))
}
Upvotes: 1