How to sort dates in Datatables
How to sort dates in Datatables
Link to test case: $("#displayJson").DataTable({
data: employs,
columns: [
{data: "Name},
{ data: "Position" },
{ data: "EmpId" },
{ data: "Organisation" },
{ data: "Place" },
{
data: "DOJ" ,
render: function(data){
return new Date(data).toLocaleFormat('en-GB')
}
},
Debugger code (debug.datatables.net):
Error messages shown: Uncaught TypeError: (intermediate value).toLocaleFormat is not a function
at render
Description of problem: I have given dates in my Datatable. But, I am facing problem in sorting the dates. so, I have given timestamps in my .json file to make sorting easier. I am importing json data to the DataTables. Now I want to display date in DD/MM/YYYY format to the end user.
My problem is how to convert milliseconds to date format to display it to the user in the datatable
Replies
The examples in this section should get you going. They're showing how you search on any date, and even convert the source date into a different format,
Colin
I got the answer without converting the date into timestamps.
I used a CDN for my HTML code:
<script src="https://cdn.datatables.net/plug-ins/1.10.11/sorting/date-eu.js"></script>
then I added a single line of code for my date column using columnDefs
columnDefs: [{
targets: [5],
type:"date-eu",
}]
That's it!