Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/** Derivative work from date-de plug-in :
* https://github.com/DataTables/Plugins/blob/master/sorting/date-de.js
*
* date and time : dd[DATE_SP]mm[DATE_SP]yy HH[TIME_SP]mm[TIME_SP]ss
* date : dd[DATE_SP]mm[DATE_SP]yy
*
* @author zyzo
* @example
* "columnDefs": [
{type : 'datetime', targets : 3},
{type : 'date', targets : 1}
],
*
**/
// Date separator
var DATE_SP = '-';
// Time separator
var TIME_SP = ':'
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"datetime-asc": function ( a, b ) {
var x, y;
if ($.trim(a) !== '') {
var deDatea = $.trim(a).split(' ');
var deTimea = deDatea[1].split(TIME_SP);
var deDatea2 = deDatea[0].split(DATE_SP);
x = (deDatea2[2] + deDatea2[1] + deDatea2[0] + deTimea[0] + deTimea[1] + deTimea[2]) * 1;
} else {
x = Infinity; // = l'an 1000 ...
}
if ($.trim(b) !== '') {
var deDateb = $.trim(b).split(' ');
var deTimeb = deDateb[1].split(TIME_SP);
deDateb = deDateb[0].split(DATE_SP);
y = (deDateb[2] + deDateb[1] + deDateb[0] + deTimeb[0] + deTimeb[1] + deTimea[2]) * 1;
} else {
y = Infinity;
}
var z = ((x < y) ? -1 : ((x > y) ? 1 : 0));
return z;
},
"datetime-desc": function ( a, b ) {
var x, y;
if ($.trim(a) !== '') {
var deDatea = $.trim(a).split(' ');
var deTimea = deDatea[1].split(TIME_SP);
var deDatea2 = deDatea[0].split(DATE_SP);
x = (deDatea2[2] + deDatea2[1] + deDatea2[0] + deTimea[0] + deTimea[1] + deTimea[2]) * 1;
} else {
x = Infinity;
}
if ($.trim(b) !== '') {
var deDateb = $.trim(b).split(' ');
var deTimeb = deDateb[1].split(TIME_SP);
deDateb = deDateb[0].split(DATE_SP);
y = (deDateb[2] + deDateb[1] + deDateb[0] + deTimeb[0] + deTimeb[1] + deTimea[2]) * 1;
} else {
y = Infinity;
}
var z = ((x < y) ? 1 : ((x > y) ? -1 : 0));
return z;
},
"date-asc": function ( a, b ) {
var x, y;
if ($.trim(a) !== '') {
var deDatea = $.trim(a).split(DATE_SP);
x = (deDatea[2] + deDatea[1] + deDatea[0]) * 1;
} else {
x = Infinity; // = l'an 1000 ...
}
if ($.trim(b) !== '') {
var deDateb = $.trim(b).split(DATE_SP);
y = (deDateb[2] + deDateb[1] + deDateb[0]) * 1;
} else {
y = Infinity;
}
var z = ((x < y) ? -1 : ((x > y) ? 1 : 0));
return z;
},
"date-desc": function ( a, b ) {
var x, y;
if ($.trim(a) !== '') {
var deDatea = $.trim(a).split(DATE_SP);
x = (deDatea[2] + deDatea[1] + deDatea[0]) * 1;
} else {
x = Infinity;
}
if ($.trim(b) !== '') {
var deDateb = $.trim(b).split(DATE_SP);
y = (deDateb[2] + deDateb[1] + deDateb[0]) * 1;
} else {
y = Infinity;
}
var z = ((x < y) ? 1 : ((x > y) ? -1 : 0));
return z;
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.