How to create an array of dates in JavaScript

3 Answers

0 votes
let dates = []; 
           
dates.push(new Date("2020/03/20")); 
dates.push(new Date("2020/05/21")); 
dates.push(new Date("2020/01/13")); 
dates.push(new Date("2020/02/27")); 
dates.push(new Date("2020/05/20")); 
 
console.log(dates);
  
  
  
/*
run:
  
[
  2020-03-20T00:00:00.000Z,
  2020-05-21T00:00:00.000Z,
  2020-01-13T00:00:00.000Z,
  2020-02-27T00:00:00.000Z,
  2020-05-20T00:00:00.000Z
]
  
*/

 



answered May 21, 2020 by avibootz
edited Apr 12, 2024 by avibootz
0 votes
let dates = ['2020/03/20', '2020/05/21', '2020/01/13', '2020/02/27', '2020/05/20']; 
          
console.log(dates);


 
/*
run:
 
 [
"2020/03/20" ,
"2020/05/21" ,
"2020/01/13" ,
"2020/02/27" ,
"2020/05/20"
] 

*/

 



answered May 21, 2020 by avibootz
edited Apr 12, 2024 by avibootz
0 votes
const dates = [new Date('2021/03/20'), new Date('2021/05/21'), new Date('2021/01/13'), 
               new Date('2021/01/14'), new Date('2022/05/20')]; 
              
  
for (let i = 0; i < dates.length; i++) {
    console.log(dates[i].toDateString());
}

   
   
   
/*
run:
   
Sat Mar 20 2021
Fri May 21 2021
Wed Jan 13 2021
Thu Jan 14 2021
Fri May 20 2022

   
*/

 



answered Apr 12, 2024 by avibootz
...