Date column sorting not working for H24 format
Date column sorting not working for H24 format
Hello,
I use a simpleDateFormat "yyyy-MM-dd hh:mm:ss.SSS" to display a date in my datatable. I've to change this format to "yyyy-MM-dd HH:mm:ss.SSS" to have the 24H format.
But with this new format, the sort on date column is not working anymore, instead of a sort by date, it is doing an alphabetical sort. Although it was working with the previous format.
It is a matter of plugin I have to use or about code ?
This is my datatable :
$('#dataTables').DataTable({
responsive: false,
ajax: {
"url": restURI + 'orch/search/events/',
"contentType": "application/json",
"type": "POST",
"data": function ( d ) {
return JSON.stringify(getDatasToSearch());
},
error: function(jqXHR, textStatus, errorThrown){
manageAjaxError(jqXHR, textStatus, errorThrown)
}
},
buttons: [
'selectAll',
'selectNone'
],
language: {
buttons: {
selectAll: "Select all items",
selectNone: "Select none"
}
},
select: {
selector: 'td:first-child',
style: 'multi'
},
order: [0, 'desc'],
scrollCollapse: true,
scrollX: false,
aLengthMenu: [
[10, 25, 50, 100, -1],
[10, 25, 50, 100, "All"]
],
iDisplayLength: 25,
"columns": [
{ "data": "EVENT_ID", "defaultContent": "", "sClass": "text-center limitedWidth" },
{ "data": "FLOW_ID", "defaultContent": "", "sClass": "text-center limitedWidth" },
{ "data": "FLOW_NAME", "defaultContent": "", "sClass": "text-center limitedWidthFlNm" },
{ "data": "OBJECT_TYPE", "defaultContent": "", "sClass": "text-center limitedWidth" },
{ "data": "OBJECT_NAME", "defaultContent": "", "sClass": "text-center limitedWidth" },
{ "data": "OBJECT_VERSION", "defaultContent": "", "sClass": "text-center limitedWidth" },
{ "data": "OBJECT_ITERATION", "defaultContent": "", "sClass": "text-center limitedWidth" },
{ "data": "OPERATION_NAME", "defaultContent": "", "sClass": "text-center limitedWidth" },
{ "data": "OPERATION_STATUS", "defaultContent": "", "sClass": "text-center limitedWidth" },
{ "data": "OPERATION_MESSAGE", "defaultContent": "", "sClass": "text-center limitedWidth" },
{ "data": "CREATE_DATE", "defaultContent": "", "sClass": "text-center limitedWidth","sType":"date" },
{ "data": "UPDATE_DATE", "defaultContent": "", "sClass": "text-center limitedWidth " ,"sType":"date" }
]
})
Thank you for your help !
This question has accepted answers - jump to:
Answers
This Date Time Sorting Blog should help.
Kevin
Thank you for your quick replay. I saw this pluging, but when I use it I have an error :
This is my datatable definition :
This is the script import :
Did I do something wrong in use ?
My guess is with something in the data being returned from the Ajax request. Not sure though.
Not that this will fix the problem but I don't think your datetime format is correct for moment.js. This [doc[(http://momentjs.com/docs/#/displaying/) contains the format strings supported. At least for the data I think you will want
YYYY-MM-DD
. Didn't check the timeAnother option would be to use [Orthogonal Data]((https://datatables.net/manual/data/orthogonal-data) and convert the datetime to a timestamp for the sort
type
.Kevin
You were right Kevin, something was wrong with the data. On java side, I've made a mistake on format, I wrote :
-YYYY-MM-DD HH:mm:ss:SSS
instead of
-YYYY-MM-DD HH:mm:ss.SSS (colon instead of dot between ss and SSS).
The plugin and the sort are working now ! Thank you for your help Kevin. Awesome !