NaN error when using datatable ordering

0

I'm trying to create a date order in a dataTable where the format will be dd-MM-aaaa hh:MM:ss .

The code below even works the problem that it gives some conversion error always in the first value that stays as NaN and when it is done the ordering stays fixed.

code:

jQuery.fn.dataTableExt.oSort['uk_date-pre'] = function (a) {
    a = a.replace("-", "/").replace("-","/");
   // a = a.slice(0, -2) + '' + a.slice(-2);
    var date = Date.parse(a);
    return typeof date === 'number' ? date : -1;
}
jQuery.fn.dataTableExt.oSort['uk_date-asc'] = function (a, b) {
    return ((a < b) ? -1 : ((a > b) ? 1 : 0));
}
jQuery.fn.dataTableExt.oSort['uk_date-desc'] = function (a, b) {
    return ((a < b) ? 1 : ((a > b) ? -1 : 0));
} 

I can not find the fault, the other data has this format.

    
asked by anonymous 11.10.2018 / 19:52

1 answer

1

Try:

jQuery.fn.dataTableExt.oSort['uk_date-pre'] = function (a) {
    var partsDate = a.split("/");
    var date = new Date(parseInt(partsDate[2], 10),
                  parseInt(partsDate[1], 10) - 1,
                  parseInt(partsDate[0], 10));


    return = typeof date === 'number' ? date : -1;
}

Example

var strDate = "21/09/2018 09:16:35"

var partsDate = strDate.split("/");
var date = new Date(parseInt(partsDate[2], 10),
  parseInt(partsDate[1], 10) - 1,
  parseInt(partsDate[0], 10));


console.log("Resultado: ", typeof date === 'number' ? date : -1);
    
11.10.2018 / 20:28