Problems when sorting date type data
Problems when sorting date type data
Hello!
First of all, this plugin is great! :)
I've implemented it several months ago on one of my projects but then have to start working on new project and my work has stopped.
Few days ago I continued working on my project and started using this plugin again. Hovewer I've noticed today that date is not sorting well.
In SQL Server (ASP.NET page) database the date is stored like this 2010-09-16 13:07:00 (YYYY-MM-DD hh:mm:ss) but my page the European version is displayed (DD.MM.YYYY).
I've tried with Ronan Guilloux version but since I had to change quite some things I've decided to try Andy McMaster ("uk version") and modified "US" version (http://datatables.net/forums/comments.php?DiscussionID=1178) but none of them work.
Even if I change
[code]
var ukDatea = a.split('/');
var ukDateb = b.split('/');
[/code]
to
[code]
var ukDatea = a.split('.');
var ukDateb = b.split('.');
[/code]
The end result is the same. If I use "default" DT plugin for date sorting looks like that day is used for sorting the dates whereas if I use "UK" and "US" plugins the dates are sorted in strange order.
My DT plugin init. code:
[code]
$('#tblDoktorji').dataTable({ "bLengthChange": true, "iDisplayLength": 50, "aaSorting": aaSorting, "aoColumns": aoColumns, "sPaginationType": "full_numbers", "aaSorting": [[0, "asc"]], "aoColumnDefs": [
{ "sType": "html", "aTargets": [0] }, { "sType": "string", "aTargets": [1, 2, 3, 4, 5] }, { "sType": "us_date", "aTargets": [6]}]
});
[/code]
Any thoughts?
Thanks!
First of all, this plugin is great! :)
I've implemented it several months ago on one of my projects but then have to start working on new project and my work has stopped.
Few days ago I continued working on my project and started using this plugin again. Hovewer I've noticed today that date is not sorting well.
In SQL Server (ASP.NET page) database the date is stored like this 2010-09-16 13:07:00 (YYYY-MM-DD hh:mm:ss) but my page the European version is displayed (DD.MM.YYYY).
I've tried with Ronan Guilloux version but since I had to change quite some things I've decided to try Andy McMaster ("uk version") and modified "US" version (http://datatables.net/forums/comments.php?DiscussionID=1178) but none of them work.
Even if I change
[code]
var ukDatea = a.split('/');
var ukDateb = b.split('/');
[/code]
to
[code]
var ukDatea = a.split('.');
var ukDateb = b.split('.');
[/code]
The end result is the same. If I use "default" DT plugin for date sorting looks like that day is used for sorting the dates whereas if I use "UK" and "US" plugins the dates are sorted in strange order.
My DT plugin init. code:
[code]
$('#tblDoktorji').dataTable({ "bLengthChange": true, "iDisplayLength": 50, "aaSorting": aaSorting, "aoColumns": aoColumns, "sPaginationType": "full_numbers", "aaSorting": [[0, "asc"]], "aoColumnDefs": [
{ "sType": "html", "aTargets": [0] }, { "sType": "string", "aTargets": [1, 2, 3, 4, 5] }, { "sType": "us_date", "aTargets": [6]}]
});
[/code]
Any thoughts?
Thanks!
This discussion has been closed.
Replies
Allan
Thank you for your fast response.
The code for plugin is followin:
[code]
jQuery.fn.dataTableExt.oSort['us_date-asc'] = function(a,b) {
var ukDatea = a.split('.');
var ukDateb = b.split('.');
var x = (ukDatea[0] + ukDatea[1] + ukDatea[2]) * 1;
var y = (ukDateb[0] + ukDateb[1] + ukDateb[2]) * 1;
if (isNaN(x) || x < y) { return -1; }
if (isNaN(y) || x > y) { return 1; }
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['us_date-desc'] = function(a,b) {
var ukDatea = a.split('.');
var ukDateb = b.split('.');
var x = (ukDatea[0] + ukDatea[1] + ukDatea[2]) * 1;
var y = (ukDateb[0] + ukDateb[1] + ukDateb[2]) * 1;
if (isNaN(y) || x < y) { return 1; }
if (isNaN(x) || x > y) { return -1; }
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
[/code]
The dates in SQL server are written like this: 2011-02-28 13:02:00.000
The dates that table receives are written as follow: 28.2.2011 11:24:00
Before date are actually shown on the page the string is edited with ASP.NET function for displaying date without time: 28.2.2011
I believe that jQ function should be written so that it edits the dates that are shown on the page not those that are written in database which are written in American way but then server (or browser or OS) shows them in European way 28.2.2011.
Thanks!
all the best