Sorting Datatables by Date and Time


Topic: Sorting Datatables by Date and Time

Paint Ball asked 6 years ago

I have a column with date and time in the following format 07/04/2019 01:14 AM

How can I have columns sort correctly for AM and PM?

If i have 3 rows and sort them they end up like this:

07/04/2019 11:14 AM // Correct

07/04/2019 01:13 AM // Correct

07/04/2019 01:25 PM // This should be first

the sorting seems to only work alphanumerically. Is there a way for it to understand its time

I also have issues with sorting items that have an element wrapping text but I can open a separate ticket for that if I need to.


You can add the following sorting method to your options to sort according to date and time , specially if your time in 12 hour(AM/PM) format. I use the following code and it works for me:

$('#tableid').DataTable({
                "order": [[, 'desc']],//order column in descending order.
                "columnDefs": [
                    { "type": "date", "targets":  }//date column formatted like "03/23/2018 10:25:13 AM".
                ],
                "pageLength": 25
            });

Aliaksandr Andrasiuk staff answered 6 years ago

HI,

Than you for the reported bug. If it worth implementing we can add it in the near future.

As you mentioned sorting only work alphanumerically. So it compares identical symbols and after founding the 11:14 it compares it with 01:25 and 01:13(given that the previous characters are the same) without considering the AM / PM part.

Can you give the example of the not working wrapped text?

In v4.15.0 we brought the new property sortRows which allows using components as fields. These components can be searchable and sortable. Maybe it will be helpful in case of wrapping text.


Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Resolved

Specification of the issue
  • User: Free
  • Premium support: No
  • Technology: MDB React
  • MDB Version: 4.17.0
  • Device: computer
  • Browser: all
  • OS: osx 10.13.6
  • Provided sample code: No
  • Provided link: No