Sorting dates in columns

Sorting dates in columns

sjw01sjw01 Posts: 67Questions: 36Answers: 1

I see this: https://datatables.net/reference/option/columns.type

From what I understand, effectively a date column can only be sorted provided they are displayed as YYYY-MM-DD?

Is that right? The "limited set of dates" link to in the option shows YYYY-MM-DD ... YYYY/MM/DD

Which effectively is just sorting numerically and doesn't really require the field type to be a date? If I put the date into a format that allows sorting numerically, there is no point to defining a column type = date?

As can be seen, http://live.datatables.net/lidequra/21/edit - all date columns are displayed DD/MM/YY and don't sort

Am I misinterpreting the doco? Displaying the date as YYYY-MM-DD is really unfriendly and I want to avoid it?

This question has an accepted answers - jump to answer

Answers

  • xtech_devxtech_dev Posts: 25Questions: 11Answers: 0
  • MSLtdMSLtd Posts: 56Questions: 5Answers: 4

    Hello @sjw01
    When you're using the DataTables date column you are limited to a very finite subset of formatting options, all of which I'm pretty sure, are in the format YYYY-MM-DD.

    If you are wishing to have a user friendly format, I'd recommend using the render attribute of the column declaration in your table initiation, to render an HTML5 Input of type: date (<input type="date" name="inputFieldName">) in the editor and pushing the information around yourself.

    In other news, I've had a play with the example and the date columns sort fine??

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

    Have a read over this blog post which goes into full details of date / time sorting in DataTables.

    Allan

  • sjw01sjw01 Posts: 67Questions: 36Answers: 1

    @MSLtd It would appear that the column is sorted using the rendered data so we're back to square one. The columns definitely don't sort as required.

    @allan thanks, i'll take a look

  • sjw01sjw01 Posts: 67Questions: 36Answers: 1
    edited August 2018

    I read over the blog post and I implemented moment exactly as described.

    1. Included JS Files:

    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
    <script type="text/javascript" src="//cdn.datatables.net/plug-ins/1.10.19/sorting/datetime-moment.js"></script>

    1. Added column.type to the specified column:

    { title: "RO Date", type: "date" },

    1. Added formatting to dataTable

    $.fn.dataTable.moment( 'DD/MM/YY' );

    Still wont sort date columns ???

    http://live.datatables.net/lidequra/30/edit

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

    You had:

    { title: "RO Date", type: "date" },

    Remove the type option. That is incorrectly forcing the data type. Allow DataTables to auto-detect the type.

    Allan

This discussion has been closed.