How change datetime formate in generated table by

2019-09-15 15:53发布

enter image description here

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条回答
三岁会撩人
2楼-- · 2019-09-15 16:24

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
        }
    }]
});
查看更多
登录 后发表回答