How to get date column to sort acs/desc when clicking on the column header
How to get date column to sort acs/desc when clicking on the column header
So I have two date fields in the datatable for status date, one for displaying data in the date format mm/dd/yyyy and the other which is hidden has a format of yyyy-mm-dd which i use for sorting (in desc order) when the table loads. So when the data loads everything looks good but when a user clicks on "status date" column header to sort by asc/desc it sorts it by month from the mm/dd/yyyy format instead of by the actual date. How do I get the displayed Status Date field to sort by date correctly when the user clicks on the column header?
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
What does your DT initialization code look like?
This blog discusses the recommended way to sort by dates.
Kevin
here you go:
$('#CustomerStatusTable').DataTable({
"order": [[5, "desc"]],
"ajax": {
"url": "/Home/GetData",
"type": "GET",
"datatype": "json",
"serverSide": true,
"processing": true
I think you're over-complicating this. When you mentioned a hidden field, I thought you had something like this:
where dob_for_display is the date formatted for your display and dob has the sortable format yyyy-dd-mm.
I looked at the blog you referenced. it wasn't so clear but I put that code from that page in my document.ready and now the datatable doesn't load
Check your browser's console for errors.
Kevin
the errors were from the moment js file. i got it working by using orderData:[5]
thank you for your help