Skip to content

Instantly share code, notes, and snippets.

@DmitriyWebDev
Created July 21, 2018 10:20
Show Gist options
  • Save DmitriyWebDev/36b48f8ce066e381f85f466b3c53a164 to your computer and use it in GitHub Desktop.
Save DmitriyWebDev/36b48f8ce066e381f85f466b3c53a164 to your computer and use it in GitHub Desktop.
Get the date with the minimum age using the Moment.js library
const dates = ["2018-07-01", "2018-02-01", "2017-01-01", "2015-07-19", "2010-07-19"];
getMinAgeDateWithMomentJs(dates); // return "2018-07-01"
function getMinAgeDateWithMomentJs(dates = []) {
if( !dates.length || typeof moment === 'undefined' ) {
console.log( 'getMinAgeDate(), invalid argument or moment.js is undefined' );
return false;
}
let result = dates.shift();
for( let i = 0; i < dates.length; i++ ) {
if( moment(result).isBefore(moment(dates[i]), 'day') ) { // or 'year', 'mohth' etc.
result = dates[i];
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment