Problem with sorting dates with jquery tablesorter
Asked Answered
E

8

21

I am using tablesorter plugin to sort my tables in an MVC .NET App. Most of my columns are strings and I'm having no problems with them. Neither with the numeric ones. The thing is my datetime columns are also getting sorted as if they were strings. They get sorted like this: 01/04/2009, 02/02/2009, 03/08/2009, etc. I obtain the data from the Model in that View.

My call is the default one:

$("#table").tablesorter();

I tried specifying dateformat with no luck:

$("#table").tablesorter({
            dateFormat: 'dd/mm/yyyy'});

The odd thing happens when I manually type a static table with random dates. It gets sorted! But my data comes from a DB call and is put into the Model, I then itreate through it and write tr's with the data.

Thanks in advance.

EDIT: Could it be something related with the way I create the tr's?

<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.Encode(item.date) %>
</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
<% } %>
Egidio answered 7/7, 2009 at 12:2 Comment(2)
Is it possible to format your date in yyyy-mm-dd?Oriya
nope, it has to be in my country's locale (Spain). :(Egidio
L
26

Try adding a Tablesorter parser to your date column. Tablesorter comes with a parser for shortDate, usLongDate and isoDate.

$("#table").tablesorter({
    headers: { colNum: { sorter: 'shortDate'} }
});

where colNum is the column with the dates. The only example I could find on the tablesorter site is here. This also works if tablesorter is sorting numbers wrong as well. There are other parsers as well for percent, ip address and more. Take a look near the end of the source code and they'll be listed there.

Edit: In looking at the source code, the dateFormat option appears to be looking only for "us", "uk", "dd/mm/yy" or "dd-mm-yy". What happens when you try "uk"?

Lettuce answered 7/7, 2009 at 12:48 Comment(6)
I tried shorDate parser aswell but forgot to mention. No luck either :( I'll give a try with the other date parsers and let you know. ThanksEgidio
That worked! I put the call as follows: $("#table").tablesorter({dateFormat: 'uk'}); I keep wondering why, with the default call, works as intended when writing a table in plain HTML and not when MVC-generating it (and having to find this workaround). Thanks!Egidio
Once again StackOverflow appears to be the only place with the correct answer! dateFormat: 'uk' works for me too, while dateFormat: "dd/mm/yyyy" doesn't.Sear
dd/mm/yyyy does not exist ;)Adenosine
@CasBloem which is lame, because their own documentation claims to support it: mottie.github.io/tablesorter/docs/…Causal
Using " dateFormat: 'ddmmyyyy' " worked for me. It doesn't understand "dd/mm/yyyy".Barby
C
16

I got the same problem, and I added a custom parser called datetime:

$.tablesorter.addParser({
    id: "datetime",
    is: function(s) {
        return false; 
    },
    format: function(s,table) {
        s = s.replace(/\-/g,"/");
        s = s.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})/, "$3/$2/$1");
        return $.tablesorter.formatFloat(new Date(s).getTime());
    },
    type: "numeric"
});

Then you just need to apply that format to the columns you want, as Gabe G exposed (For example to assign this sorter to the first column you should do the following:

$("#mytable").tablesorter( 
    {   dateFormat: 'dd/mm/yyyy', 
        headers: 
            {
                0:{sorter:'datetime'}
            } 
    } ); 
Croner answered 16/11, 2010 at 17:57 Comment(0)
S
9

You can also add a hidden span tag before your date in numerical format (yyyymmdd). This text will come first and be used for sorting but it will be hidden from sight and only show the format you want.

    <td><span style="display:none">20130923</span>23 September 2013</td>    
Safar answered 27/2, 2013 at 14:40 Comment(1)
It would be more logical to use a data-* attribute on the table cell, rather than having a span as a child element.Parnassus
I
2

There exist an update for jquery tablesorter plugin.

According to the locale of the your application, you can sort the dates by this update.

You can view the update of the tablesorter by following the below link.

http://tablesorter.openwerk.de/

Intermediacy answered 16/6, 2011 at 7:27 Comment(0)
G
0

Easier way use:

dateFormat:'mm/dd/yyyy hh:mm:ss'
Gooding answered 12/7, 2013 at 10:35 Comment(0)
P
0

To be honest the simplest solution for me was, as compsmart said, adding some hidden text in front of the actual date.

  • dateFormat: 'uk' was not working for me, maybe because my date format is again different
  • http://tablesorter.openwerk.de/ involves modifying the CSS, first I don't understand why and second the effort is bigger than simple adding hidden text in front of the date.

I like the KISS solution from compsmart!

Pontifical answered 25/9, 2013 at 12:12 Comment(0)
M
0

http://mottie.github.io/tablesorter/docs/

Set the date format. Here are the available options. (Modified v2.0.23).

  • "mmddyyyy" (default)
  • "ddmmyyyy"
  • "yyyymmdd"

In previous versions, this option was set as "us", "uk" or "dd/mm/yy". This option was modified to better fit needed date formats. It will only work with four digit years!

The sorter should be set to "shortDate" and the date format can be set in the "dateFormat" option or set for a specific columns within the "headers" option. See the demo page to see it working.

$(function(){
  $("table").tablesorter({

    dateFormat : "mmddyyyy", // default date format

    // or to change the format for specific columns,
    // add the dateFormat to the headers option:
    headers: {
      0: { sorter: "shortDate" }, // "shortDate" with the default dateFormat above
      1: { sorter: "shortDate", dateFormat: "ddmmyyyy" }, // day first format
      2: { sorter: "shortDate", dateFormat: "yyyymmdd" }  // year first format
    }

  });
}); 

Individual columns can be modified by adding the following (they all do the same thing), set in order of priority (Modified v2.3.1):

  • jQuery data data-dateFormat="mmddyyyy".
  • metadata class="{ dateFormat: 'mmddyyyy'}". This requires the metadata plugin.
  • headers option headers : { 0 : { dateFormat : 'mmddyyyy' } }.
  • header class name class="dateFormat-mmddyyyy". Overall dateFormat option.

In my case I have used

$("#myTable").tablesorter({dateFormat: "uk"}) 

for the version.

Muir answered 2/3, 2015 at 14:33 Comment(0)
S
0

This was the answer for me:

<td data-order=<fmt:formatDate pattern = "yyyy-MM-dd" value = "${myObject.myDate}" />>${myObject.myDate}</td>

more details, here: https://datatables.net/manual/data/

Sciurine answered 27/8, 2019 at 20:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.