Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong sorting for dd.mm.yyyy format #149

Open
AndiLeni opened this issue Sep 30, 2018 · 1 comment
Open

Wrong sorting for dd.mm.yyyy format #149

AndiLeni opened this issue Sep 30, 2018 · 1 comment

Comments

@AndiLeni
Copy link

Hello,

While using the script for my site I saw that the following dates are not sorted in the right order:
18.02.2018

11.02.2018
16.01.2018
18.02.2018

Is there a fix for this?
Using 'uk' as a parameter didn't work.

Regards
Andi

@Mottie
Copy link
Collaborator

Mottie commented Sep 30, 2018

The built-in "shortDate" parser only works with these formats dd-mm-yyyy or dd/mm/yyyy, not dd.mm.yyyy.

You can add your own custom date parser like this (demo):

$(function() {

  $.tablesorter.addParser({
    id: "ddmmyyyy",
    is: function(s) {
      return /\d{1,2}[\.\/\-]\d{1,2}[\.\/\-]\d{2,4}/.test(s);
    },
    format: function(s, table) {
      var c = table.config,
        // reformat the string in ISO format
        d = s.replace(/(\d{1,2})[-.\/](\d{1,2})[-.\/](\d{4})/, "$3/$2/$1"),
        date = new Date(d);
      console.log(d, date)
      return date instanceof Date && isFinite(date) ? date.getTime() : s;
    },
    type: "numeric"
  });

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants