0

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.

3
  • I think that this is what you are looking for How to sort an object array by date property? Commented Sep 30, 2021 at 10:39
  • Just a hint. Try 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. Commented Sep 30, 2021 at 10:41
  • Your problem is using the built–in parser to parse an unsupported timestamp format. You can write a simple function to parse the timestamps correctly, then sort as (a, b) => myParse(a) - myParse(b). Commented Sep 30, 2021 at 11:11

4 Answers 4

0

The problem is that Date uses a yyyy/mm/dd format instead of a dd/mm/yyyy format. So all your Date objects are Invalid Date.

P.S. If you sort but have to reverse the order afterwards, just reverse the subtractions.

Sign up to request clarification or add additional context in comments.

3 Comments

Not all dates are invalid, only the 1st and the 3rd. Although even 2nd and 4th are not parsed as expected.
I think it's mostly a miracle that 2/4 even parse to begin with if the format isn't correct.
Parsing any string other than those supported by ECMA-262 (essentially the formats produced by date.toISOString and date.toString) with the built–in parser is implementation dependent and should be strongly discouraged.
0

if momentjs an option:

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) => moment(b,"DD-MM-YYYY hh:mm:ss") - (moment(a,"DD-MM-YYYY hh:mm:ss")));
console.log(x)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

Comments

0

I think this is a very hard task when you try to compare date and time in strings. And would take to much time when executing. Instead i would convert date and times to timestamp format then compare. To convert a date string to a timestamp format i referred to the answer on this stackoverflow question

const dates = [ '29/09/2020 11:55:56', '04/08/2021 11:57:06', '30/09/2019 15:19:49', '04/08/2021 13:57:06' ];
dates.sort((date1,date2) => {
    var dateTimeParts = date1.split(' '),
    timeParts = dateTimeParts[1].split(':'),
    dateParts = dateTimeParts[0].split('/'),
    date1;

    date1 = new Date(dateParts[2], parseInt(dateParts[1], 10) - 1, dateParts[0],         timeParts[0], timeParts[1], timeParts[2]);
    
    var dateTimeParts2 = date2.split(' '),
    timeParts2 = dateTimeParts2[1].split(':'),
    dateParts2 = dateTimeParts2[0].split('/'),
    date2;

    date2 = new Date(dateParts2[2], parseInt(dateParts2[1], 10) - 1, dateParts2[0],         timeParts2[0], timeParts2[1], timeParts[2]);
    return date1.getTime() / 1000 - date2.getTime() / 1000
});

console.log(dates);

1 Comment

Consider let [D,M,Y, H, m, s] = '29/09/2020 11:55:56'.split(/\W/); let d = new Date(Y, M-1, D, H, m, s). :-)
0

Date and time are not separate in the context of a javascript Date object, internally they are just stored as millisecond offsets. But, you can't rely on Date parsing strings for you. Write a custom parser for you specific date-time strings and then then simply subtract dates.

There's no need for reverse() either, simply switch the subtraction to change direction.

const 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' ];

const parse_date_string = (str) => {
  const [date, time]= str.split(' ');
  const [day, month, year] = date.split('/');
  const [hr, min, sec] = time.split(':');
   
  return [year, (month - 1), day, hr, min, sec].map(Number);
}

x.sort((a, b) => new Date(...parse_date_string(b)) - new Date(...parse_date_string(a)));


console.log(x)

2 Comments

Why not let [day, month, year, hr, min, sec] = str.split(/\W/)? Don't forget to subtract 1 from the (calendar) month. :-)
Fair points, I was going for clarity in the splits for the OP's sake, but the month is def an oversight.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.