DataTables date sorting issue

DataTables date sorting issue

cbarrettcbarrett Posts: 9Questions: 2Answers: 1
edited July 2017 in Free community support

I have a json that I call and parse: JSON

The JSON returns a date/time in epoch. I convert the epoch using the following method:

var d = new Date(forecastFeatures[i].attributes.FORECAST_HOUR);
        var forecastHour = d.toUTCString();
        var dateString = forecastHour;
        dateString = dateString.split(' ').slice(0, 5).join(' ')

This returns me the following: Fri, 07 Jul 2017 02:00:00

That is the exact format I want. I do a console.log to view the forecast hours and the forecast hour returns in ascending order. Datatables is not sorting the data based on the return of the JSON call.

function renderDataTable() {
        $('#forecast-table').DataTable({
            destroy: true,
            searching: false,
            scrollX: false,
            scrollY: "400px",
            scrollCollapse: false,
            paging: false,
            responsive: true,
            info: false,
            autoWidth: true,
            footer: false
        });
    }

How do I sort a datatable based on the order the JSON returns?

Answers

  • kthorngrenkthorngren Posts: 21,592Questions: 26Answers: 5,006
    edited July 2017

    Many people use the plugn described in this blog for date time sorting:
    https://datatables.net/blog/2014-12-18

    Maybe it will work for you.

    Kevin

  • cbarrettcbarrett Posts: 9Questions: 2Answers: 1

    @kthorngren thank for you the reference! I was able to perform a work-around.

    var timestamp = forecastFeatures[i].attributes.FORECAST_HOUR;
    var dateString = moment(parseInt(timestamp)).startOf('hour').add(5, 'hours').format("MMMM D YYYY, HH:mm");
    

    So that the DataTable sorts based on the month, then day, then time.

This discussion has been closed.