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 get the maximum date from an array of dates in JavaScript

3 Answers

0 votes
function maxDate(dates) {
    let max = dates[0],
    obj = new Date(dates[0]);

    dates.forEach(function(dt, index) {
      if (new Date( dt ) > obj) {
        max = dt;
        obj = new Date(dt);
      }
    });
 return max;
}

let dates = ['2020/03/20', '2020/05/21', '2020/01/13', '2020/01/14', '2020/05/20']; 
          
console.log(maxDate(dates));
 
 
/*
run:
 
2020/05/21

*/

 




answered May 21 by avibootz
0 votes
let dates = ['2020/03/20', '2020/05/21', '2020/01/13', '2020/01/14', '2020/05/20']; 

let max = Date(Math.max.apply(null, dates));

console.log(max);
 
 
/*
run:
 
Thu May 21 2020 10:09:20 GMT+0300 (Israel Daylight Time)

*/

 




answered May 21 by avibootz
0 votes
let dates = ['2020/03/20', '2020/05/21', '2020/01/13', '2020/01/14', '2020/05/20']; 
 
let max = dates.reduce(function(a, b) { return a > b ? a : b; });
 
console.log(max);
  
  
/*
run:
  
2020/05/21
 
*/

 




answered May 21 by avibootz
...