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