Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and work.

aviboots(AT)netvision.net.il

Instant Grammar Checker - Correct all grammar errors and enhance your writing
BlueHost Web Hosting

Bonsai Boy of New York Bonsai Trees and Accessories

Website Domains Names & Hosting | Domain.com

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting


Liquid Web Cloud VPS Hosting

Disclosure We are a professional Programming & Software Q&A website, that receives compensation from some of the links whose we show you with Information. It means that my content may contain affiliate links.

13,285 questions

18,078 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 03:00:00 GMT+0300 (Israel Daylight Time)" ,
"Sat May 02 2020 03:00:00 GMT+0300 (Israel Daylight Time)" ,
"Sun May 03 2020 03:00:00 GMT+0300 (Israel Daylight Time)" ,
"Mon May 04 2020 03:00:00 GMT+0300 (Israel Daylight Time)" ,
"Tue May 05 2020 03:00:00 GMT+0300 (Israel Daylight Time)" ,
"Wed May 06 2020 03:00:00 GMT+0300 (Israel Daylight Time)" ,
"Thu May 07 2020 03:00:00 GMT+0300 (Israel Daylight Time)" ,
"Fri May 08 2020 03:00:00 GMT+0300 (Israel Daylight Time)" ,
"Sat May 09 2020 03:00:00 GMT+0300 (Israel Daylight Time)" ,
"Sun May 10 2020 03:00:00 GMT+0300 (Israel Daylight Time)" ,
"Mon May 11 2020 03:00:00 GMT+0300 (Israel Daylight Time)" ,
"Tue May 12 2020 03:00:00 GMT+0300 (Israel Daylight Time)" ,
"Wed May 13 2020 03:00:00 GMT+0300 (Israel Daylight Time)" ,
"Thu May 14 2020 03:00:00 GMT+0300 (Israel Daylight Time)"
] 

*/

 




answered May 21 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:
 
[
Fri May 01 2020 03:00:00 GMT+0300 (Israel Daylight Time) ,
Sat May 02 2020 03:00:00 GMT+0300 (Israel Daylight Time) ,
Sun May 03 2020 03:00:00 GMT+0300 (Israel Daylight Time) ,
Mon May 04 2020 03:00:00 GMT+0300 (Israel Daylight Time) ,
Tue May 05 2020 03:00:00 GMT+0300 (Israel Daylight Time) ,
Wed May 06 2020 03:00:00 GMT+0300 (Israel Daylight Time) ,
Thu May 07 2020 03:00:00 GMT+0300 (Israel Daylight Time) ,
Fri May 08 2020 03:00:00 GMT+0300 (Israel Daylight Time) ,
Sat May 09 2020 03:00:00 GMT+0300 (Israel Daylight Time) ,
Sun May 10 2020 03:00:00 GMT+0300 (Israel Daylight Time) ,
Mon May 11 2020 03:00:00 GMT+0300 (Israel Daylight Time) ,
Tue May 12 2020 03:00:00 GMT+0300 (Israel Daylight Time) ,
Wed May 13 2020 03:00:00 GMT+0300 (Israel Daylight Time) ,
Thu May 14 2020 03:00:00 GMT+0300 (Israel Daylight Time)
] 
 
*/

 




answered May 21 by avibootz
...