Waltham WECAN
Waltham WECAN

Reputation: 491

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).

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?

Image of the list

Upvotes: 0

Views: 15890

Answers (5)

Andreas Tzionis
Andreas Tzionis

Reputation: 1086

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'))

Upvotes: 0

Aurel B&#237;l&#253;
Aurel B&#237;l&#253;

Reputation: 7973

When 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);

Upvotes: 3

Shawn Whinnery
Shawn Whinnery

Reputation: 3677

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/

Upvotes: 1

Scott Selby
Scott Selby

Reputation: 9570

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. .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);
 }

Upvotes: 2

tiagodws
tiagodws

Reputation: 1395

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.

Upvotes: 1

Related Questions