Dates formatting and dates sorting do not work when loading data in AJAX

Dates formatting and dates sorting do not work when loading data in AJAX

AntelionAntelion Posts: 9Questions: 1Answers: 0
edited December 2019 in Free community support

Problem
I cannot find a completely satisfactory solution to the problem of date formatting AND date sorting.
I simply cannot make the sorting work other than with a purely ISO 8601 date format.

Previous threads on the topic
1) https://datatables.net/forums/discussion/42601/how-to-format-date-in-table-cell#Comment_111314 (date formatting)
2) https://datatables.net/forums/discussion/45692/how-to-date-sort-as-date-instead-of-string#Comment_120818 (date sorting)

My situation
I am loading JSON data in AJAX and I have got a column containing dates in ISO 8601 standard: 2019-12-31T11:30:00
I have applied solution 1) and I can do whatever I want with the dates formatting. Problem is, sorting does not work at all.

You can see it by yourself here:
http://live.datatables.net/ciwumehe/1/edit

Dates are treated as mere strings.

Solution 2) simply does not work for me. Following those instructions nothing happens to the dates.

Question
Is there a way to make work both dates formatting and sorting while loading data in AJAX ?

To be more clear
I want to display 31/12/2019 11:30
I want to sort 2019-12-31T11:30:00

Thanks

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,166Questions: 26Answers: 4,921
    Answer ✓

    To display the in the format you want you will need to use the correct moment date/time format as documented here:
    https://momentjs.com/docs/#/displaying/

    To display 31/12/2019 11:30 you will need the format DD/MM/YYYY HH:mm.

    Next you will want to follow the steps in this datetime sorting blog. I updated you test case with both changes.

    http://live.datatables.net/ciwumehe/803/edit

    Kevin

  • AntelionAntelion Posts: 9Questions: 1Answers: 0

    @kthorngren

    Great example, thanks.

    I have read that the default dates format for Data Tables is ISO 8601.
    I see that your code shows a different one.

    Why the solution does not work if you express dates in ISO 8601 format ?

  • kthorngrenkthorngren Posts: 21,166Questions: 26Answers: 4,921
    Answer ✓

    Why the solution does not work if you express dates in ISO 8601 format ?

    Should work if you specify the formats correctly. Please provide an example of ISO 8601 formats not working so we can take a look.

    Kevin

  • AntelionAntelion Posts: 9Questions: 1Answers: 0

    I checked it again with an ISO 8601 date: 2019-12-31T11:30:00. It works perfectly. Thanks.

This discussion has been closed.