Find the closest date in the future

Here’s a 5-minute video on how to use the Custom JavaScript pipe to find the closest date in the future.

Code:

const dates = [
  '{d1}',
  '{d2}',
  '{d3}',
  '{d4}'
];

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;
   }
});

if(true){
 closest
}
3 Likes