Skip to content Skip to sidebar Skip to footer

Sorting Date And Time With AM | PM In Jquery

I have my javascript to sort date in ascending(that is, arrange from the newest). I am able to do for the dates, but i couldnt sort the time with AM or PM. I can do it in 24 hours

Solution 1:

you can use following code you need to fix date format first date1.replace("pm"," PM").replace("am"," AM") before parsing it into date to compare it

function sortAscending(a, b) {
    var date1 = $(a).find("time").text();

    var date2 = $(b).find("time").text();

    date1 = Date.parse(date1.replace("pm"," PM").replace("am"," AM"));

    date2 = Date.parse(date2.replace("pm"," PM").replace("am"," AM"));

   return new Date(date1 > date2);

}

$(document).ready(function() {
    $('#wrapper .cards').sort(sortAscending).appendTo('#wrapper');
});

output it will be

02
2012/04/20 02:41am
01
2012/04/20 10:25am
04
2012/04/20 10:45am
03
2012/04/20 07:00pm

Post a Comment for "Sorting Date And Time With AM | PM In Jquery"