Created
July 21, 2018 10:20
-
-
Save DmitriyWebDev/36b48f8ce066e381f85f466b3c53a164 to your computer and use it in GitHub Desktop.
Get the date with the minimum age using the Moment.js library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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