سؤال

I'm using tablesorter to add sorting to some html tables however I have a column with UK dates and times, it seem to sort OK on the date portion but is ignoring the time.

Data:

27/01/2014 22:26:02
27/01/2014 22:26:01
27/01/2014 22:26:03

Html:

<tr>
    <th>Document</th>
    <th class="headerSortUp {sorter:'ukLongDate'}">Creation Date</th>
 </tr>  

I know you can add a custom parser like :

ts.addParser({
    id: "ukLongDate",
    is: function (s) {
        return s.match(new RegExp(/^[A-Za-z]{3,10}\.? [0-9]{1,2}, ([0-9]{4}|'?[0-9]{2}) (([0-2]?[0-9]:[0-5][0-9])|([0-1]?[0-9]:[0-5][0-9]\s(AM|PM)))$/));
    }, format: function (s) {
        return $.tablesorter.formatFloat(new Date(s).getTime());
    }, type: "numeric"
});

What should the regexp be?

هل كانت مفيدة؟

المحلول

Try this parser (demo):

$.tablesorter.addParser({
    id: "ukLongDate",
    is: function (s) {
        return false;
    }, format: function (s) {
        s = s.replace(/(0?[1-9]|[12][0-9]|3[01])\/(0?[1-9]|1[012])\/(\d{4})/, '$2/$1/$3');
        return $.tablesorter.formatFloat(new Date(s).getTime());
    }, type: "numeric"
});

$('table').tablesorter({
    headers : {
        2 : { sorter: 'ukLongDate' }
    }
});

I didn't use the metadata method from the question <th class="{sorter:'ukLongDate'}"> in case you happen to be missing the metadata plugin.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top