How to sort dates in Datatables

How to sort dates in Datatables

ChandrikaChandrika Posts: 2Questions: 0Answers: 0

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

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    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

  • ChandrikaChandrika Posts: 2Questions: 0Answers: 0

    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! :)

Sign In or Register to comment.