Date Ordering Seems Off When a String

Date Ordering Seems Off When a String

DanJ210DanJ210 Posts: 23Questions: 5Answers: 0

I hope you guys/girls aren't getting tired of me yet. But this is a good one.

So I'm wanting to order columns by date but it seems the dates are being ordered incorrectly. I'll go from October to November but in November the days will be mixed. So 10/13/2017 to 11/15/2017 to 11/3/2017 to 11/8/2017. If I reverse the order by clicking on the header I'll get 11/8/2017 first, then 11/3/2017 and then 11/15/2017. Shouldn't I get 3 -> 8 -> 15? Here's the data code because I am rendering these into date objects and then to locale strings.

I'm assuming that since I'm taking the date object and turning it into a string, that's why this odd ordering is happening. This is the string that the "LastCommunication" column is using to create the date object, "2017-11-15T13:41:35.2653148-05:00"

var workerDataTable = $('#wTable').DataTable({
            "ajax": {
                url: "/Home/returnWorkerJson",
                dataSrc: function (data) { return JSON.parse(data); }
            },
            columns: [
                { data: "Name" },
                { data: "ServerName" },
                { data: "Attributes.Client" },
                { data: "Attributes.ver" },
                {
                    data: "LastCommunication",
                    render: function (data, type, row)
                    {
                        //var newTime = data.slice(0, 19);
                        var newDate = new Date(data);
                        return newDate.toLocaleString();
                    }
                },
                { data: "TelephonyStatistics.BY" },
                { data: "TelephonyStatistics.DA" },
                { data: "TelephonyStatistics.DL" },
                { data: "TelephonyStatistics.FX" },
                { data: "TelephonyStatistics.NR" },
                { data: "TelephonyStatistics.OI" },
                { data: "TelephonyStatistics.TO" },
                { data: "TelephonyStatistics.WA" },
                { data: "TelephonyStatistics.WL" },
                { data: "TelephonyStatistics.UNK" },
                { data: "WorkItemsGiven" },
                { data: "WorkItemsReturned" }
            ]
        });

How can we order dates if they can't be strings?

Answers

  • kthorngrenkthorngren Posts: 20,255Questions: 26Answers: 4,761

    See if this blog helps:
    https://datatables.net/blog/2014-12-18

    Kevin

  • DanJ210DanJ210 Posts: 23Questions: 5Answers: 0

    This look pretty promising. Thanks for the great support and I hope I'm not taking advantage of it. I'll report back if this is a solution which I'm sure it is.

  • DanJ210DanJ210 Posts: 23Questions: 5Answers: 0

    This is going to take me a bit to figure out so I'll come back to this as soon as I can.

  • allanallan Posts: 61,627Questions: 1Answers: 10,090 Site admin

    I have another option for you, using orthogonal data:

                    {
                        data: "LastCommunication",
                        render: function (data, type, row)
                        {
                            //var newTime = data.slice(0, 19);
                            var newDate = new Date(data);
                            return type === 'sort' || type === 'type' ?
                              newDate.getTime() :
                              newDate.toLocaleString();
                        }
                    },
    

    That will return an integer (epoch milliseconds) if DataTables needs type or sort information, otherwise it will use the locale string - e.g. for display and filtering.

    Lots of options :).

    Allan

This discussion has been closed.