Date sorting with moment does not work
Date sorting with moment does not work
Link to test case:
http://jsfiddle.net/kwe217qu/
Debugger code (debug.datatables.net):
Error messages shown:
None
Description of problem:
I'm getting data through AJAX and trying to fix formatted date sorting for a column. I looked around the web and I feel like I'm doing everything right with the moment.js stuff, but for some reason it continues to sort the formatted dates incorrectly as if they're just strings.
How do I get the dates to sort correctly when they're in ddd, MMM Do h:mm A format (ex: Sat, Jan 8th 3:23 PM)?
This question has an accepted answers - jump to answer
Answers
Thats an interesting problem. I think I just learned something new about Datatables. Looks like, when the column type is string Datatables sets the orthogonal data for the
sort
operation to lower case so that the case doesn't affect sorting. In your test case the the sort string is set to lower case, ie,wed, dec 29th 8:37 am
so it doesn't match the format you set.I added a second column that uses orthogonal data to set the
display
andfilter
operations to the format you specify but leaves thesort
with the original data.http://jsfiddle.net/Ltyn86q1/
You will see this in the console output:
column 0 type: string
column 1 type: date
Sort Data:
0: "wed, dec 29th 8:37 am"
1: 1640785032570
And you will see that the second column sorts properly.
Kevin
Kevin, your help is invaluable. Thank you very much! My table is sorting properly now and we learned something new.