uniDate — universal date sorting plugin
uniDate — universal date sorting plugin
https://github.com/shvchk/uniDate
Configurable, everything is optional, works for any all-numeric dates with non-numeric separators.
Minified version available: https://github.com/shvchk/uniDate/blob/master/uniDate.min.js — 543B (326B gzipped).
[code]
function uniDate(date) {
// split date by any non-digit character
var inputDate = date.split(/\D+/g);
/**
* Configure this to suit your needs!
*
* Your input date has its elements in particular order, and here
* you should describe it.
*
* E.g., if you have date like '2011.11', i.e. no days, no hours,
* nothing but year and month, you should set year's position to 1,
* month's position to 2, and all other's positions to whatever you
* like (I suggest 0 or 9).
*
* By default we assume your date to be in the following order:
* 'day.month.year hour:minute:second', but don't forget that any
* non-digit can be used as separator, so even 'day/month.year
* hour%minute::second' is ok :)
*
* So let's assume we have something like 24.12.2012 17:47:39.
*
* 24 . 12 .2012 17 : 47 : 39
* ^ ^ ^ ^ ^ ^
* day.month.year hour:minute:second
* ^ ^ ^ ^ ^ ^
* 1 2 3 4 5 6
*
* Here is how its description looks like:
*/
var universalDate = {
'year': 3,
'month': 2,
'day': 1,
'hour': 4,
'minute': 5,
'second': 6
};
/**
* That's it, no more configuration required.
*
* Don't change anything after this line,
* unless you know what you are doing.
*/
$.each(universalDate, function(key, value) {
if (inputDate[value - 1]) {
universalDate[key] = inputDate[value - 1].toString();
if (universalDate[key].length == 1) {
universalDate[key] = 0 + universalDate[key];
}
} else {
universalDate[key] = 0;
}
});
return (universalDate.year + universalDate.month + universalDate.day + universalDate.hour + universalDate.minute + universalDate.second) * 1;
}
jQuery.fn.dataTableExt.oSort['uniDate-asc'] = function(a, b) {
x = uniDate(a);
y = uniDate(b);
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['uniDate-desc'] = function(a, b) {
x = uniDate(a);
y = uniDate(b);
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
[/code]
Configurable, everything is optional, works for any all-numeric dates with non-numeric separators.
Minified version available: https://github.com/shvchk/uniDate/blob/master/uniDate.min.js — 543B (326B gzipped).
[code]
function uniDate(date) {
// split date by any non-digit character
var inputDate = date.split(/\D+/g);
/**
* Configure this to suit your needs!
*
* Your input date has its elements in particular order, and here
* you should describe it.
*
* E.g., if you have date like '2011.11', i.e. no days, no hours,
* nothing but year and month, you should set year's position to 1,
* month's position to 2, and all other's positions to whatever you
* like (I suggest 0 or 9).
*
* By default we assume your date to be in the following order:
* 'day.month.year hour:minute:second', but don't forget that any
* non-digit can be used as separator, so even 'day/month.year
* hour%minute::second' is ok :)
*
* So let's assume we have something like 24.12.2012 17:47:39.
*
* 24 . 12 .2012 17 : 47 : 39
* ^ ^ ^ ^ ^ ^
* day.month.year hour:minute:second
* ^ ^ ^ ^ ^ ^
* 1 2 3 4 5 6
*
* Here is how its description looks like:
*/
var universalDate = {
'year': 3,
'month': 2,
'day': 1,
'hour': 4,
'minute': 5,
'second': 6
};
/**
* That's it, no more configuration required.
*
* Don't change anything after this line,
* unless you know what you are doing.
*/
$.each(universalDate, function(key, value) {
if (inputDate[value - 1]) {
universalDate[key] = inputDate[value - 1].toString();
if (universalDate[key].length == 1) {
universalDate[key] = 0 + universalDate[key];
}
} else {
universalDate[key] = 0;
}
});
return (universalDate.year + universalDate.month + universalDate.day + universalDate.hour + universalDate.minute + universalDate.second) * 1;
}
jQuery.fn.dataTableExt.oSort['uniDate-asc'] = function(a, b) {
x = uniDate(a);
y = uniDate(b);
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['uniDate-desc'] = function(a, b) {
x = uniDate(a);
y = uniDate(b);
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
[/code]
This discussion has been closed.
Replies
i.e.
uniDate("2012-12-24 17:47:39", [1, 2, 3, 4, 5, 6]);
uniDate("21/1 12:00", [3, 2, 4, 5]); // assume current year, seconds = 00
I'd use this.
Regards,
Allan
i.e.
uniDate("2012-12-24 17:47:39", ["year", "month", "day", "hours", "minutes", "seconds"]);
uniDate("2012-12-24 17:47:39", ["yr", "mo", "day", "hour", "min", "sec"]); // abbrev
uniDate("2012-12-24 17:47:39", ["y", "m", "d", "h", "i", "s"]); // php date format abbrevs
mapping["year"] = mapping["yr"] = mapping["y"] = 1;
mapping["month"] = mapping["mo"] = mapping["m"] = 2;
//etc
http://datatables.net/release-datatables/examples/plug-ins/sorting_plugin.html
http://datatables.net/release-datatables/examples/plug-ins/sorting_sType.html
In this case, the code above is all for the sorting plug-in, so you need to include that (after you include the DataTables script, but before you initialise your table) and set the sType for the columns you want to sort in this manner (the second example), since there is no automatic type detection for this plug-in.
Allan
[code] // pull AM/PM value off the end. get the last 2 characters.
var ampm = date.substring(date.length - 2, 2);
// split date by any non-digit characters
var inputDate = date.split(/\D+/g);
if (ampm == "PM") {
inputDate[3] = inputDate[3] + 12;
}
[/code]
[code]
"aoColumnDefs": [
{"bSortable": false, "aTargets": [0,1,2,3,4]},
{"sType": "uniDate", "aTargets": ["date"]},
{"sType": "numeric", "aTargets": ["numeric"]}
],
[/code]
Still having no luck on the sorting. Actually, it sorts fine unless I have enough records to force an ajax call back to the server. Then the sorting is off. Might be going at this the wrong way.
Allan
Part 2: in order to get the date data to show correctly and formatted as desired, I was formatting in both the SQL and the html using jsp import="java.text.SimpleDateFormat". The order by on the SQL was generated using the formatted SQL column so the data ordered based upon the formatting. Revising.
My date sorting is now going OK. But I am doing my sorting using mysql ORDER BY statements. (The ajax call calls the my sql where I change the WHERE and ORDER BY based upon the users selections. If the overall result set is < 250 rows, I set a flag to tell datatables not to use serverside when resorting. If > 250 rows, when sorting, I go back to the database.
Are there examples of code for sorting server side? I mean the java code.