Datetime column does not sort correctly
Datetime column does not sort correctly
FranciscoDC
Posts: 2Questions: 1Answers: 0
Hello in my HTML table I have a date column whose value is in ISO 8601 format for example 2023-02-30T07:45:00-03:00, but for some reason DataTables cannot sort this column correctly, in ascending order the datetime 2022-01-11T20:22:00-03:00 is coming first than 2022-01-05T06:20:00-03:00, which is completely wrong. How can I solve this problem?
new DataTable(table, {
paging: false,
ordering: true,
responsive: true,
columnDefs: [
{
targets: 4,
type: "date",
},
],
order: [4, 'asc']
});
Answers
Possibly you will need to use
DataTable.datetime()
to define the format. See the date ordering examples found here.Kevin
Yes, DataTables' default doesn't support timezones I'm afraid. You need to add a data type for the format as Kevin indicates.
Allan
I simplified my datetime to dd/MM/yyyy HH:mm (example 10/24/2023 6:47 PM) and used DataTable.datetime('dd/MM/yyyy HH:mm'); but it still didn't work.
Are you using the Luxon or Moment library?
I don't think your format string matches using either library. For example you have
PM
as part of the date time value but your format does not haveA
if using Moment nora
if using Luxon. Take a closer look at the appropriate example for the library you are using:https://datatables.net/examples/datetime/order-moment.html
https://datatables.net/examples/datetime/order-luxon.html
There is a link to the formatting docs.
Kevin