Find the next closest date in MM/DD/YYY format JavaScript

There is no need for a sorting algorithm. You only need to iterate once and find the closest date that is greater or equals today.

Pseudocode

closest <- infinity
foreach date in dates:
    if (date >= now and date < closest) then
        closest <- d
return closest

JavaScript

const dates = [
  '2/3/2015',
  '7/5/2015',
  '1/21/2016',
  '2/19/2016',
  '7/1/2016',
  '10/22/2019',
  '08/12/2019',
];

const now = new Date();

let closest = Infinity;

dates.forEach(function(d) {
   const date = new Date(d);

   if (date >= now && (date < new Date(closest) || date < closest)) {
      closest = d;
   }
});

console.log(closest);


Personally I would use a library such as the very good Moment.JS library, to handle all the horrible complexity of dates.

It has a difference method:

http://momentjs.com/docs/#/displaying/difference/

e.g.

var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b) // 86400000

It would then be trivial to Math.min() the differences of each date in your list.

There's also a moment.min, which might shortcut this entirely, if all your dates are in the future already:

http://momentjs.com/docs/#/get-set/min/