jQuery date sorting

34,763

Solution 1

I don't know much about plugins...although if you want a light weight method without plugins, you may try this:

html:

<div id="D">
    <div class="dateDiv">2012-04-15 10:25:45</div>
    <div class="dateDiv">2012-04-10 19:41:08</div>
    <div class="dateDiv">2012-04-20 07:00:10</div>
    <div class="dateDiv">2012-04-12 16:45:50</div>
</div>

For js:

var elems = $.makeArray($(".dateDiv"));
elems.sort(function(a, b) {
    return new Date( $(a).text() ) < new Date( $(b).text() );
});
$("#D").html(elems);

UPDATE:
Thanks to CMS for answering this question about parsing dates: Why does Date.parse give incorrect results?

function parseDate(input) {
  var parts = input.match(/(\d+)/g);
  // new Date(year, month [, date [, hours[, minutes[, seconds[, ms]]]]])
  return new Date(parts[0], parts[1]-1, parts[2], parts[3], parts[4], parts[5]); //     months are 0-based
}

var elems = $.makeArray($(".dateDiv"));
elems.sort(function(a, b) {
    console.log( parseDate( $(a).text() ) );
    return parseDate( $(a).text() ) < parseDate( $(b).text() );
});
$("#D").html(elems);​

Now, don't tell me this doesn't work...hehe

Solution 2

You can use the date.js library combined with this sorter function to do this:

$('#dateDiv').sortElements(function(a, b){
    return Date.parse($(a).text()) > Date.parse($(b).text()) ? 1 : -1;
});

The original repo has not been maintained, but an updated fork of it appears to be in progress here.

Share:
34,763
Sandeep Kumar
Author by

Sandeep Kumar

Updated on February 20, 2020

Comments

  • Sandeep Kumar
    Sandeep Kumar about 4 years

    Possible Duplicate:
    Javascript date sorting by convert the string in to date format

    I am not good in jquery so I was wondering if there is a way or plugin I can use to sort date divs. I have date in YYYY:MM:DD HH:MM:SS format. I am displaying date in divs as shown below. Divs are in unordered format and I want to sort them on latest date first basis.

    <div id="dateDiv">2012-04-15 10:25:45</div>
    <div id="dateDiv">2012-04-10 19:41:08</div>
    <div id="dateDiv">2012-04-20 07:00:10</div>
    <div id="dateDiv">2012-04-12 16:45:50</div>
    

    Thanks

  • strah
    strah almost 12 years
    This won't work - this is not a valid string for Date constructor.
  • Parth Thakkar
    Parth Thakkar almost 12 years
    I don't know why...but this works: jsfiddle.net/jCkCP
  • strah
    strah almost 12 years
    It doesn't work for me: jsfiddle.net/WpTb5/1 Maybe it's browser / OS dependent?
  • Parth Thakkar
    Parth Thakkar almost 12 years
    i've updated the answer...this should definitely work
  • strah
    strah almost 12 years
    Nope... console.log(Date.parse( $(a).text() ));//NaN. UPDATE: That's browser dependent: works in Chrome, doesn't in Firefox.
  • PinnyM
    PinnyM almost 12 years
    In theory, there's no reason to parse out a date since the format used will always sort as text just fine. Not very futureproof, but it will work...
  • Julian Dormon
    Julian Dormon about 10 years
    This worked well for me but I found the Date.js was much slower parsing the dates than using the parseDate function by Parth Thakkar above. Hope that helps someone.
  • The Onin
    The Onin about 8 years
    That library was last updated 9 years ago.
  • PinnyM
    PinnyM about 8 years
    @NinoŠkopac see github.com/datejs/Datejs
  • PinnyM
    PinnyM about 8 years
    updated link to indicate this