Datetime column not sorting correctly

Datetime column not sorting correctly

cj1005cj1005 Posts: 144Questions: 46Answers: 1

Link to test case: https://www.wd4g.com/WCGateway/DTDemoCase1.wc
Debugger code (debug.datatables.net): n\a
Error messages shown: n\a
Description of problem: See below

Hi, this is my first attempt at having a datetime column, so I'm probably missing something obvious, but I've searched the forums and implemented the suggestions I found, but it is not quite right.

The column searches correctly on the date but the times are not right, it should be the most recent at the top, this is the code I have added, so far:

Before the DT init section I added:

    $.fn.dataTable.moment('DD/MM/YYYY'); \\ This already existed and is needed for simple date fields
    $.fn.dataTable.moment('DD/MM/YYYY HH:MM:SS A'); \\ This I added for the new datetime fields

In the column definition section I added:

     { targets: [0], type: "moment-DD/MM/YYYY HH:MM:SS A"},

If someone could please take a look and advise that would be greatly appreciated.

Thanks, Chris

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,324Questions: 26Answers: 4,949
    edited 12:04AM Answer ✓

    This is an example of your datetime value:

    21/11/2024 09:25:47 AM
    

    The format of DD/MM/YYYY HH:MM:SS A is not correct. See the moment display docs for the correct formatting tokens. I believe the format should be this:

    DD/MM/YYYY HH:mm:ss A
    

    Note the mm for minutes and ss for seconds.

    Kevin

  • kthorngrenkthorngren Posts: 21,324Questions: 26Answers: 4,949

    Also it's not recommended to set the columns.type. Datatables type detection will set the type appropriately as long as all the values in the column match the format. If there is a mismatch then type detection will default to string regardless of the type: "moment-DD/MM/YYYY HH:MM:SS A" setting. Remove columns.type from the config.

    Kevin

  • cj1005cj1005 Posts: 144Questions: 46Answers: 1

    That works, thank you so much Kevin :smile:

Sign In or Register to comment.