Created
April 9, 2018 16:46
-
-
Save bkawan/96764b99c77ad6e3e79b25af409b7d2b to your computer and use it in GitHub Desktop.
Sorting date with javascript
This file contains 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
let unsortedDates = | |
['2018-01-26', | |
'2018-01-30', | |
'2018-02-13', | |
'2018-02-20', | |
'2018-02-27', | |
'2018-03-01', | |
'2018-03-17', | |
'2018-03-21', | |
'2018-03-06', | |
'2018-03-19', | |
'2018-03-22']; | |
// lets convert to Date object first | |
unsortedDates = unsortedDates.map(date => new Date(date)); | |
// lets sort date now | |
let sortedDates = unsortedDates.sort(function (a, b) { | |
return a.getTime() - b.getTime() | |
}); | |
// lets convert date object to date string | |
sortedDates = sortedDates.map(x => x.getUTCFullYear() + "-" + (x.getUTCMonth() + 1) + "-" + x.getUTCDate()); | |
console.log(sortedDates) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment