Date sorted as string - how can I make it sort correctly?

Date sorted as string - how can I make it sort correctly?

sanelosanelo Posts: 2Questions: 1Answers: 0

Hello,

I apologize if this is a repeat but I was not able to find a solution in my search.

I am populating my datatable dynamically through an object. The html is static with an empty body. The date I am sending back from the server is in this format (string) and the format I need it to display in:

01/26/2017 07:00:12 AM // MM/DD/YYYY HH:MM:SS 12

Here is how I am loading the data into the datatable:

// For each row, add data dynamically to table
        $.each(transactionDetails, function(index, item) {
            table.row.add([
                        item.applicationName,
                        item.apiName,
                        item.transactionId,
                        item.reqTimeStamp, // This is the date string
        });

        // Redraw the table
        table.draw();

When I want to sort the date, its sorted in string format (from left to right) and this causes incorrect sorting.

How can I sort the date correctly while displaying it in the above format?

Thanks in advance.

Sanel

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Alternatively - just my own preference - it is logical to be returning a timestamp for sorting. You could then have a "hidden" column for the timestamp, and sort column "formatted date for display" by your hidden column "timestamp".

  • sanelosanelo Posts: 2Questions: 1Answers: 0

    @kdpowell

    Your answer may be what I need, but how would I add data-attributes to the row with code provided above? Right now I'm simply returning the visible output of each row, but I am not sure how I would add a data attribute to it.

    Thanks.

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    My post of 6th Feb was wrong. I was thinking of UNIX timestamps.
    My apologies.

This discussion has been closed.