Date/time sorting not working

Date/time sorting not working

galcottgalcott 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.

Answers

  • kthorngrenkthorngren Posts: 20,344Questions: 26Answers: 4,776

    You will need to double check your moment format with this doc. For example MM expects 07 for the month but you have 7 which should be M. Same with the hours. Not sure about the days. Your AM/PM should be A. I think the format should be either mm/dd/YYYY hh:mmA or mm/DD/YYYY hh:mmA depending on your days format.

    Kevin

  • galcottgalcott Posts: 53Questions: 15Answers: 1

    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.

Sign In or Register to comment.