Skip to content Skip to sidebar Skip to footer

How To Sort Dates Below 1970 In Javascript?

so I am sorting dates and there is one date of 1967-08-07, what would be the correct approach to it? The unix timestamp is negative and I haven't found any clues how to do in JS.

Solution 1:

The ISO 8601

1967-08-07

structure is easy sortable as string, because it has the year as first part, followed by month and day.

Solution 2:

If the dates are of different formats that are not easily compared, you could convert the dates to date objects and compare those:

// CREATE OBJECTSvar dateOne = newDate("October 16, 2017");
var dateTwo = newDate("1967-08-07");

// COMPAREif(dateOne > dateTwo){

   // DO SOMETHING

}

Post a Comment for "How To Sort Dates Below 1970 In Javascript?"