Search code examples
javascripttablesorter

Tablesorter and date mm/yyyyy


I've been using tablesorter a lot in my website like this

$(".tablesorter").tablesorter({
                widgets: ['zebra'], 
                dateFormat: 'uk', 
});

But i've a problem with date in the format : MM/YYYY (it's when I don't have the day I prefer showing 12/2005 than 00/12/2005).

This format does not work with tablesorter dateFormat uk. How to make it working ?

And the difficulty is that I can have in the same table differents formats like this :

Date - Title
11/2005 - Movie 1
12/11/2005 - Movie 2
2006 - Movie 3

Thank you.


Solution

  • You could add a custom parser:

    $.tablesorter.addParser({ 
        id: 'rough-date', 
        is: function() { 
            // return false so this parser is not auto detected 
            return false; 
        }, 
        format: function(s) { 
            // format to look like an ISO 8601 date (2005-11-12).
            // Month-only dates will be formatted 2005-11.
            return s.split('/').reverse().join('-');
        }, 
        type: 'text' // sort as strings
    });
    

    And then use it like this (where 0 is the column number that contains the date):

    $(".tablesorter").tablesorter({ 
        headers: { 
            0: {
                sorter:'rough-date' 
            } 
        } 
    });
    

    Here's a JSFiddle.