Sort date field with tablesorter

I am using the jQuery tablesorter plugin. The table has a column that shows the dates in a format 05 Mar 2012. The tablesorter plugin seems to treat this column as text because it sorts it in order

  • March 05, 2012
  • Jan 6, 2012
  • Dec 07, 2012

How can I sort these dates in chronological order instead?

+2
source share
4 answers

Divide the date string by the date, and then convert it to milliseconds. Let tablesorter sort the column as numeric.

$.tablesorter.addParser({ 
    id: 'my_date_column', 
    is: function(s) { 
        // return false so this parser is not auto detected 
        return false; 
    }, 
    format: function(s) { 
        var timeInMillis = new Date.parse(s);
        return timeInMillis;         
    }, 
    // set type, either numeric or text 
    type: 'numeric' 
}); 

$(function() { 
    $("table").tablesorter({ 
        headers: { 
            6: {       // Change this to your column position
                sorter:'my_date_column' 
            } 
        } 
    }); 
});

If you are having problems with Date.parse, see my answer to this question .

+3
source

span (yyyymmdd). , .

    <td><span style="display:none">20130923</span>23rd September 2013</td>
+3

addParser , .

http://tablesorter.com/docs/example-parsers.html

:

http://www.datejs.com/

EDIT: :

 console.log(new Date('05 Mar 2012'))//  logs proper date object
+1

tablesorter , , , , .

dateformat tablesorter .

dateFormat: 'dd MMM yyyy' 
0

All Articles