Date/time sorting not working
Date/time sorting not working
galcott
Posts: 53Questions: 15Answers: 1
I'm having a problem sorting a date/time column correctly. It's in US format. Records within the same day are not sorting correctly by time, as shown in these screen shots of ascending and descending sorts.
Here are the scripts I'm including.
<script src="https://cdn.datatables.net/v/dt/dt-1.10.18/datatables.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
<script src="http://cdn.datatables.net/plug-ins/1.10.25/sorting/datetime-moment.js"></script>
In the document.ready function I have this code to set the format, which seems correct.
$.fn.dataTable.moment('MM/DD/YYYY HH:mma');
So I can't figure out why the time sort is wrong.
This discussion has been closed.
Answers
You will need to double check your moment format with this doc. For example
MM
expects07
for the month but you have7
which should beM
. Same with the hours. Not sure about the days. Your AM/PM should beA
. I think the format should be eithermm/dd/YYYY hh:mmA
ormm/DD/YYYY hh:mmA
depending on your days format.Kevin
You're mostly correct, thanks! All the fields work the same way - a double letter requires a leading zero, a single letter doesn't. But there's no lower case mm, and lower case dd gives you an abbreviation for the day name rather than the number, so those were correct as I had them.