Date column sorts alphabetically instead of chronologically in DataTables
0 reputation · 25 Apr 2026, 15:15 UTC
0 reputation · 25 Apr 2026, 15:15 UTC
The goal is a DataTables 1.13+/2.x table that displays UTC timestamps converted to each viewer's regional time zone while the date column still sorts chronologically. DataTables performs no native date parsing — cell values are handled as strings — so a column holding formatted local dates can fall back to lexicographic ordering, which scatters non-ISO formats out of sequence.
Two documented mechanisms compete for this role. The datetime-moment plugin teaches DataTables to parse and format dates through Moment.js, while orthogonal data — a render function returning a display string alongside a hidden sort value, or a data-order attribute — keeps the raw timestamp for ordering. The regional conversion itself must happen during data transformation before the cell renderer runs, and doing it client-side makes the output depend on the visitor's system clock and locale being configured correctly. Moment.js also adds bundle weight and is in maintenance mode, which keeps Luxon in the comparison.
The unresolved decision is which layer should own conversion and ordering:
35275 reputation · 25 Apr 2026, 21:44 UTC
To maintain chronological sorting while displaying regional dates, you must separate the sort value (the raw UTC timestamp) from the display value (the formatted local string). DataTables sorts based on the value it is given; if that value is a formatted string like "October 12", it will sort alphabetically.
datetime-moment plugin provides formatting helpers, but it does not automatically solve sorting if the render function only returns a formatted string. You still need to provide a sortable value.render function. This ensures the raw data remains untouched for sorting and filtering, while the viewer's browser handles the regional offset.render function in your column definition to return different values based on the type parameter:{
data: 'timestamp',
render: function (data, type, row) {
if (type === 'sort' || type === 'type') {
return data; // Return raw UTC timestamp for chronological sorting
}
// Convert to local time for display only
// Using native Intl.DateTimeFormat as a lightweight alternative to Moment.js
return new Intl.DateTimeFormat(navigator.language, {
dateStyle: 'medium',
timeStyle: 'short'
}).format(new Date(data));
}
}data-order attribute to the <td>:<td data-order="1728854000">Oct 13, 2026, 6:32 PM</td>If you are using server-side processing (serverSide: true), the sorting happens on the database level, not the client. In this case, please confirm if your API response includes the raw timestamp in a separate field for the server to order by.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.