I Know there are lot of answers out there to sort date and time. But in every answer, they sort only date or they sort only time. I can't find the answer which sorts both date and time string.
Here's the Code I tried:
var x = [ '29/09/2020 11:55:56', '04/08/2021 11:57:06', '30/09/2019 15:19:49', '04/08/2021 13:57:06' ]
x.sort((a, b) => new Date(b).getTime() - new Date(a).getTime()).reverse();
console.log(x)
Output I got:
["04/08/2021 13:57:06", "30/09/2019 15:19:49", "04/08/2021 11:57:06", "29/09/2020 11:55:56"]
Required Output:
["04/08/2021 13:57:06","04/08/2021 11:57:06", "29/09/2020 11:55:56", "30/09/2019 15:19:49", ]
I searched whole day. Please Help me with some solutions to fix this.
[ new Date('29/09/2020 11:55:56'), new Date('04/08/2021 11:57:06'), new Date('30/09/2019 15:19:49'), new Date('04/08/2021 13:57:06') ]and you get this[Invalid Date, Thu Apr 08 2021 11:57:06 GMT+0200 (Central European Summer Time), Invalid Date, Thu Apr 08 2021 13:57:06 GMT+0200 (Central European Summer Time)]so there's no surprise it doesn't work as you expect since there are 2 invalid dates. All you need to do is to fix the date string format. Your code should work just fine.(a, b) => myParse(a) - myParse(b).