Sort a column according date in luxon format "dd.MM.yyyy" with data from csv does not work

Sort a column according date in luxon format "dd.MM.yyyy" with data from csv does not work

rootjsrootjs Posts: 3Questions: 1Answers: 0

Link to test case: https://jsfiddle.net/rootjs/4hn13rbz/
Debugger code (debug.datatables.net): n/a
Error messages shown: none
Description of problem: Row "0" should be recognized as luxon date format and should be sorted according this date format. For some reason this doesn't work, it's just sorted in lexicographical order, which is wrong.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 19,864Questions: 26Answers: 4,685
    edited October 2023 Answer ✓

    Since the CSV date format matches the DataTable.datetime('dd.MM.yyyy'); format you defined you don't need to use the date renderer, for example:

    render: DataTable.render.datetime('dd.MM.yyyy')
    

    However if you want to use the date renderer you need to supply the to, from and locale parameters. By default the renderer looks for an ISO-8601 date which you don't have. You will need this:

    render: DataTable.render.datetime('dd.MM.yyyy', 'dd.MM.yyyy', 'en')
    

    See the Date Renderer docs for details.

    Updated example:
    https://jsfiddle.net/15Lbhaj0/

    Kevin

  • rootjsrootjs Posts: 3Questions: 1Answers: 0

    Thanks a lot Kevin! With the additional function parameters for the date renderer / datetime the sorting works :)
    I had a look in the Date Renderer docs again and I think the relation to sorting / correct date format recognition and the number of parameters is not obvious there.
    If I get it correctly DataTable.render.datetime(to) which I used is only helpful when the table contains ISO-8601 date data and the aim is to format this data to another format.

    Maybe the documentation:

    from the date / time format that should be parsed from the column. This also uses the Moment.js and Luxon parsing tokens, and note that the parsing is strict - it must exactly match the format of your date.

    might be enhanced by a hint like this:
    "Use from parameter to correctly recognize non ISO-8601 date formats and enable sorting of these formats."

  • allanallan Posts: 60,903Questions: 1Answers: 9,927 Site admin

    If I get it correctly DataTable.render.datetime(to) which I used is only helpful when the table contains ISO-8601 date data and the aim is to format this data to another format.

    Correct.

    Good call on the documentation clarification. I've phrased it slightly different, but with the same idea:

    from the date / time format that should be parsed from the column - this is required when the detection of non-ISO8601 date/time formats is needed.

    I've got it in git and it will be deployed with the next site update.

    Thanks!
    Allan

  • rootjsrootjs Posts: 3Questions: 1Answers: 0

    Thanks Allan for picking this up. I like the library and how it's maintained :)

Sign In or Register to comment.