How to date sort as date instead of string
How to date sort as date instead of string
I never noticed this until I actually needed to sort by a date. All my dates are in USA format (dd/mm/yyyy) but sort as they are strings. From what I read in various forum answers, I was under the impression that DataTables automatically recognized this as a date format. See screenshot for example.
This is all the code I have. Let's say the date column is column 5. How would I force recognition of that column as a date column so that it sorts as a date and not a string?
$(() => {
$(".display").DataTable({
dom: 'Bfrtip',
scrollX:true,
buttons: [{
extend: 'excelHtml5',
exportOptions: {
columns: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
}
}]
});
});
This question has an accepted answers - jump to answer
Answers
Due to how Chrome parses dates, the latest DataTables core only really supports ISO8601 format out of the box. For anything else, this is the best option.
Allan
Just a follow up. The use of moment.js solved my problem.
It took me a few moments to realize that I needed to place a local copy of
//cdn.datatables.net/plug-ins/1.10.16/sorting/datetime-moment.js
into my local folder as we don't use CDNs in our system. Once that detail was squared away, it was really easy to get the dates and time sorting as dates instead of strings.I think a way better solution is to use the "sort" data attribute, like this :
I'm using Erb templates so code between <%= %> is server code.
My solution for PHP is like this:
notice that the formats of data-sort attr value $mydate and <td> inner value $newformat are different. Hope it will help somebody.
you can sort table like this date wise
@srturgut, your solution worked perfectly for me.
@srturgut , that is a simple solution that worked for me. Thank you
Because I am loading the data as json, I don't have the option of setting td attributes.
I solved it by using a js function utilizing moment. This function will format the date based on the current locale, but sort based on the raw date input YYYY-MM-DD
Complete working example:
This worked a treat, thanks!
For us, the approach is to use a microformat like this
<time datetime="2020-02-10">10.02.2020</time>
. The microformat makes sure that DataTables sees a sortable version and humans see theirs.However, DataTables detects this cell type as a number and incorrectly sorts just by the visual numbers anyways, so we have to override the auto-detection via the
columns
array. See line 39 in this example. You won't need theorderDataType
, just thetype
key.I tried Winkbrace solution which was working until it break for some unknown reasons.
According to https://datatables.net/reference/option/columns.render , in render function, the type parameter is important, and I changed the date render function to :
You can just return the data if type is equal to 'sort'. I choose to return the timestamp, thinking the sort will be faster.
On php, one solution could be to run the date into time using strtotime function.
The ASP.NET equivalent of the answer from @srturgut is as follows:
strDate_Format_ISO = "{0:yyyy-MM-dd HH:mm}"
strUser_Date_Format = "{0:dd-MMM-yyyy HH:mm}"
<td data-sort="<%# Eval("My_Date_Field", strDate_Format_ISO) %>">
<%# Eval("My_Date_Field", strUser_Date_Format)%>
</td>