jQuery date sorting
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.
Sandeep Kumar
Updated on February 20, 2020Comments
-
Sandeep Kumar about 4 years
Possible Duplicate:
Javascript date sorting by convert the string in to date formatI 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 almost 12 yearsThis won't work - this is not a valid string for Date constructor.
-
Parth Thakkar almost 12 yearsI don't know why...but this works: jsfiddle.net/jCkCP
-
strah almost 12 yearsIt doesn't work for me: jsfiddle.net/WpTb5/1 Maybe it's browser / OS dependent?
-
Parth Thakkar almost 12 yearsi've updated the answer...this should definitely work
-
strah almost 12 yearsNope... console.log(Date.parse( $(a).text() ));//NaN. UPDATE: That's browser dependent: works in Chrome, doesn't in Firefox.
-
PinnyM almost 12 yearsIn 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 about 10 yearsThis 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 about 8 yearsThat library was last updated 9 years ago.
-
PinnyM about 8 years@NinoŠkopac see github.com/datejs/Datejs
-
PinnyM about 8 yearsupdated link to indicate this