Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,560 questions

51,419 answers

573 users

How to create an array of dates between a start and end date in JavaScript

2 Answers

0 votes
let start = new Date("2020-05-01");
let end   = new Date("2020-05-14");
let arr   = [];
 
for (let d = start; d <= end; d.setDate(d.getDate() + 1)) {
    arr.push(d.toString());
}

console.log(arr);

  
  
/*
run:
 
[
  'Fri May 01 2020 00:00:00 GMT+0000 (Coordinated Universal Time)',
  'Sat May 02 2020 00:00:00 GMT+0000 (Coordinated Universal Time)',
  'Sun May 03 2020 00:00:00 GMT+0000 (Coordinated Universal Time)',
  'Mon May 04 2020 00:00:00 GMT+0000 (Coordinated Universal Time)',
  'Tue May 05 2020 00:00:00 GMT+0000 (Coordinated Universal Time)',
  'Wed May 06 2020 00:00:00 GMT+0000 (Coordinated Universal Time)',
  'Thu May 07 2020 00:00:00 GMT+0000 (Coordinated Universal Time)',
  'Fri May 08 2020 00:00:00 GMT+0000 (Coordinated Universal Time)',
  'Sat May 09 2020 00:00:00 GMT+0000 (Coordinated Universal Time)',
  'Sun May 10 2020 00:00:00 GMT+0000 (Coordinated Universal Time)',
  'Mon May 11 2020 00:00:00 GMT+0000 (Coordinated Universal Time)',
  'Tue May 12 2020 00:00:00 GMT+0000 (Coordinated Universal Time)',
  'Wed May 13 2020 00:00:00 GMT+0000 (Coordinated Universal Time)',
  'Thu May 14 2020 00:00:00 GMT+0000 (Coordinated Universal Time)'
]

*/

 



answered May 21, 2020 by avibootz
edited Apr 16, 2025 by avibootz
0 votes
let start = new Date("2020-05-01");
let end   = new Date("2020-05-14");
let arr   = [];
 
do {
   arr.push(new Date(start));
   start.setDate(start.getDate() + 1);
} while(start <= end)

console.log(arr);

  
  
/*
run:
 
[
  2020-05-01T00:00:00.000Z,
  2020-05-02T00:00:00.000Z,
  2020-05-03T00:00:00.000Z,
  2020-05-04T00:00:00.000Z,
  2020-05-05T00:00:00.000Z,
  2020-05-06T00:00:00.000Z,
  2020-05-07T00:00:00.000Z,
  2020-05-08T00:00:00.000Z,
  2020-05-09T00:00:00.000Z,
  2020-05-10T00:00:00.000Z,
  2020-05-11T00:00:00.000Z,
  2020-05-12T00:00:00.000Z,
  2020-05-13T00:00:00.000Z,
  2020-05-14T00:00:00.000Z
]

*/

 



answered May 21, 2020 by avibootz
edited Apr 16, 2025 by avibootz
...