Datatable wrong sorting with datetime-moment

Datatable wrong sorting with datetime-moment

msm_baltazarmsm_baltazar Posts: 59Questions: 22Answers: 0
edited August 2020 in Free community support

hi friends, i can't sort c # datetime in datatable. Where's my fault?

` var dataTable = $("#tblUserAndAuthorization").DataTable({
"ajax": {
"url": "/SharingAreaCustomer/GetCustomerRelatedFiles",
"type": "POST",
"datatype": "json",
"data": { __RequestVerificationToken: token, "Id": '@Model.SharingArea.Id' }
},
"columnDefs": [
{
"targets": 0,
"visible": false,
"searchable": false
},
{
"targets": 1,
"visible": false,
"searchable": false
},
{ "width": "8%", "targets": 2, "type": "datetime-moment" }
],
"responsive": true,
'autoWidth': false,
"order": [[2, "desc"]],
"columns": [
{ "data": "Id" },
{ "data": "File.Id" },
{
"data": "UploadDate",
"render": function (data) {
return moment(date).format("DD.MM.YYYY HH:mm:ss");
}
}
]
});``

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin
    Answer ✓

    Your rendering function is returning a date / time format that DataTables doesn’t understand as a date / time - it is just seeing a string, hence the issue.

    I would suggest using the plug-in introduced in this blog post.

    Allan

  • msm_baltazarmsm_baltazar Posts: 59Questions: 22Answers: 0

    Thank you very much. I did not know about Ultimate date / time. It worked successfully. But what should I do if I want to format 2 date columns differently.

    working version as follows;
    ``
    $.fn.dataTable.moment('DD.MM.YYYY HH:mm:ss');
    var dataTable = $("#tblUserAndAuthorization").DataTable({
    "ajax": {
    "url": "/SharingAreaCustomer/GetCustomerRelatedFiles",
    "type": "POST",
    "datatype": "json",
    "data": { __RequestVerificationToken: token, "Id": '@Model.SharingArea.Id' }
    },
    "columnDefs": [
    {
    "targets": 0,
    "visible": false,
    "searchable": false
    },
    {
    "targets": 1,
    "visible": false,
    "searchable": false
    },
    { "width": "8%", "targets": 2, "type": "datetime-moment" }
    ],
    "responsive": true,
    'autoWidth': false,
    "order": [[2, "desc"]],
    "columns": [
    { "data": "Id" },
    { "data": "File.Id" },
    {
    "data": "UploadDate",
    "render": function (data) {
    return moment(date).format("DD.MM.YYYY HH:mm:ss);
    }
    }
    ]
    });```

  • kthorngrenkthorngren Posts: 20,322Questions: 26Answers: 4,773
    edited August 2020 Answer ✓

    Add $.fn.dataTable.moment('....'); for each format. Datatables will check each column to determine the column type including the all the $.fn.dataTable.moment('....'); you define.

    Kevin

  • msm_baltazarmsm_baltazar Posts: 59Questions: 22Answers: 0

    @kthorngren @allan Thank you very much to both of you. You are great.

This discussion has been closed.