Changing Date / Time messes with column sorting

Changing Date / Time messes with column sorting

Aaron55Aaron55 Posts: 2Questions: 1Answers: 0

Hello, currently my table column with date/time sort fine (format 2020-05-07T09:15:18). I want to change the format to UK gmt format, ive tried using moment.js by calling moment(data) with different formats but the column does not sort. any ideas please?

var eventsTable = $('#eventsTable').DataTable({
                    "ajax": {
                        "url": "https://localhost:5001/api/v1/ws4api/getallevents/" + ws4Id,
                        "dataSrc": "",
                        'beforeSend': function (request) {
                            request.setRequestHeader("Authorization", 'Bearer ' + localStorage.getItem('accessToken'));
                        },
                        "statusCode": {
                            401: function (xhr, error, thrown) {
                                //    window.location.href = "Index.html"
                                return false
                            }
                        }
                    },
                    "pageLength": 50,
                    "destroy": true,
                    "order": [[3, "desc"]],
                    "columns": [
                        { data: 'user' },
                        { data: 'description' },
                        { data: 'location' },
                        {
                            "data": 'time',
                            "orderable": true,
                            "render": function (data, type, row) {
                                return data;
                            }
                        },
                        { data: 'auxInfo' }
                    ]
                })

Answers

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

    I don't see your use of moment anywhere. We need to see that aspect of your code.

  • Aaron55Aaron55 Posts: 2Questions: 1Answers: 0

    Please see below:

    var dateFormat = "DD-MM-YYYY HH:mm:ss";
    
    "columns": [
                            { data: 'user' },
                            { data: 'description' },
                            { data: 'location' },
                            {
                                "data": 'time',
                                "orderable": true,
                                "render": function (data, type, row) {
                                    return moment(data, dateFormat);
                                }
                            },
                            { data: 'auxInfo' }
                        ]
    
    
  • kthorngrenkthorngren Posts: 21,171Questions: 26Answers: 4,922

    See if this blog about datetime sorting helps,

    Kevin

This discussion has been closed.