Date Sorting for MM/DD/YYYY Failing
Date Sorting for MM/DD/YYYY Failing
My page has a table that should initialize with the record create date in descending fashion. This was implemented the last quarter of 2017 but today I see that 01/02/2018 is being sorted as older than the December 2017 dates (attached screenshot). It is an Intranet site so I cannot offer a link to it.
I am using the plug in that should enable the sort [line 6 below in the code snippet])
$.ajax(
{
data: { startDt: $('#startDt').val(), endDt: $('#endDt').val() },
datatype: 'json',
success: function (rosterData) {
$.fn.dataTable.moment('MM/DD/YYYY','en-US');
$('#tblRoster').DataTable({
data: JSON.parse(rosterData.firstChild.textContent),
The column that is being sorted has the following configuration:
columns: [
{
'data': 'CreatedDt' ,
"type": "date ",
"render": function (value) {
if (value === null) return "";
return window.moment(value).format('MM/DD/YYYY');
}
},
A snippet of the JSON being used as data for the table is also attached. Thanks for any insight as to why it seems to be sorting more in string than date fashion.
This question has an accepted answers - jump to answer
Answers
Built a test case for you using your data and config snippets:
http://live.datatables.net/laximahi/1/edit
Found that you have an extra space here:
"type": "date ",
Remove the space and it should sort correctly, like the test case.
Also, it looks like you are using Bootstrap but also including extra Datatables CSS files. You have duplicate sorting arrows; one Bootstrap and one Datatables. For Bootstrap 3 you should include
dataTables.bootstrap.min.css
but notjquery.dataTables.min.css
. More details here:https://datatables.net/manual/styling/bootstrap
Kevin
If you are using the Moment plug-in for DataTables you shouldn't actually specify the
columns.type
option at all. The plug-in adds automatic type detection for the format that was specified.Allan
Thank you folks - removing the "type" attribute from the column configuration or correcting the space in it both resulted in resolution of the date sort issue. Cheers!