How change datetime formate in generated table by datatable.js?

别说谁变了你拦得住时间么 提交于 2019-12-13 07:29:16

问题


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
        }
    }]
});


来源:https://stackoverflow.com/questions/40973829/how-change-datetime-formate-in-generated-table-by-datatable-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!