How change datetime formate in generated table by

2019-09-15 16:39发布

问题:

I have displayed a datatable using datatable.js . it shows datetime field shown as in YYYY-MM-DD HH:MM:SS , but i want to change this generate time format to dd-mm-yyyy

回答1:

You can format how the date is rendered in DataTable by using render option. In render you can define your custom way to display the date, or use Date.toLocaleDateString method to format it. MDN reference: Date.prototype.toLocaleDateString()

var payload = [{ date: new Date() }];

var table = $('#table').DataTable({

    data: payload,

    columns: [{ data: 'date', title: 'Date' }],

    "columnDefs": [{
        "targets": 0,    // column index, 0 is the first column
        "type" : "date",
        "render": function (data) {
            // US English uses month-day-year order
            var date = new Date(data);
            return date.toLocaleDateString('en-US'); // 4/25/2018
        }
    }]
});