Date time with am pm sorting with moment not working with no leading day digit

Date time with am pm sorting with moment not working with no leading day digit

lancewlancew Posts: 10Questions: 3Answers: 0

I have dates I am trying to sort but it looks like 1 digit days are treated differently than 2 digit days.

Here is an example of how the dates are sorting.

Here is the moment setup I am using.
$.fn.dataTable.moment('M/d/yyyy hh:mm:ss A');

I also tried, $.fn.dataTable.moment('MM/dd/yyyy hh:mm:ss A');
They both seem to work the same way which is not correct.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin
    Answer ✓

    The Moment.js documentation shows that D should be used for day of the month - not d.

    Try:

    $.fn.dataTable.moment( 'M/D/YYYY h:mm:ss A');
    

    Allan

  • lancewlancew Posts: 10Questions: 3Answers: 0

    That works great. Thanks!

  • srilu90srilu90 Posts: 1Questions: 0Answers: 0

    I have tried the same fix given by Allan, but its not working for me. Issue is the same. It sorts dates with 2 digit days properly, but when it comes to single digit days, it considers 2 as 20 instead of 02 and is sorting it that way. Any other solution?

  • kthorngrenkthorngren Posts: 21,300Questions: 26Answers: 4,945
    edited June 2018

    It works here:
    http://live.datatables.net/juqapuzo/1/edit

    Did you load the moment.js and datetime-moment.js?

    Does you date time format match the format specified in this:
    $.fn.dataTable.moment( 'M/D/YYYY h:mm:ss A');

    EDIT: If your format doesn't match 'M/D/YYYY h:mm:ss A' then you will need to change it per the moment.js docs to match. It needs to match exactly, not just the D.

    Maybe you can update my example with an example using your datetime data/

    Kevin

This discussion has been closed.