format json date to mm/dd/yy format before displaying in a jquery datatable
For dot.net and javascript, you can just use like @David Sopko
{
"data": "Date", "type": "date ",
"render": function (value) {
if (value === null) return "";
var pattern = /Date\(([^)]+)\)/;//date format from server side
var results = pattern.exec(value);
var dt = new Date(parseFloat(results[1]));
return dt.getDate() + "." + (dt.getMonth() + 1) + "." + dt.getFullYear();
}, "autoWidth": true
},
For a nullable date time, DateTime?, you will want to use a different rendering function:
$('#userData').DataTable({
columns: [
{ "data": "userId"},
{"data": "userCreated",
"type": "date ",
"render":function (value) {
if (value === null) return "";
var pattern = /Date\(([^)]+)\)/;
var results = pattern.exec(value);
var dt = new Date(parseFloat(results[1]));
return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();}
}
]};
You can use "render" property to format your column display http://datatables.net/reference/option/columns.render#function.
For example:
{
"data": "createdTime",
"render": function (data) {
var date = new Date(data);
var month = date.getMonth() + 1;
return (month.toString().length > 1 ? month : "0" + month) + "/" + date.getDate() + "/" + date.getFullYear();
}
}
i have created demo using moment js and use of render function to convert json data into required format.
jsfiddle demo
also find code below:
testdata = [{
"id": "58",
"country_code": "UK",
"title": "Legal Director",
"pubdate": "1422454697373",
"url": "http://..."
}, {
"id": "59",
"country_code": "UK",
"title": "Solutions Architect,",
"pubdate": "1422454697373",
"url": "http://..."
}];
$('#test').dataTable({
"aaData": testdata,
"aoColumns": [{
"mDataProp": "id"
}, {
"mDataProp": "country_code"
}, {
"mDataProp": "title"
}, {
"mDataProp": "pubdate"
}, {
"mDataProp": "url"
}],
"columnDefs": [{
"targets": 3,
"data": "pubdate",
"render": function (data, type, full, meta) {
console.log('hi...');
console.log(data);
console.log(type);
console.log(full);
console.log(meta);
return moment.utc(data, "x").toISOString();
}
}]
});