Nan error when using datatable sorting

Asked

Viewed 31 times

0

I’m trying to create a date ordering 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 at the first value that gets like NaN and when the ordination is done it is 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));
} 

inserir a descrição da imagem aqui

I am not able to locate the fault, and the other data has this format.

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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.