在 jquery 数据表中显示之前将 json 日期格式化为 mm/dd/yy 格式
format json date to mm/dd/yy format before displaying in a jquery datatable
我正在尝试在 datatable 中显示一些数据,而我使用的 table 脚本是
$('#userData').dataTable({
"ajax": {
"url": "my-url",
"dataSrc": "",
},
"columns":[
{"data": "userId"},
{"data": "applicationId"},
{"data": "username"},
{"data": "firstName"},
{"data": "userCreated"},
{"data": "createdTime"},
{"data": "updatedTime"}
],
});
table 接收到的数据是 json,类似于
[
{
"userId":179,
"applicationId":"pgm-apn",
"username":"collaborator.user3",
"password":"password1",
"email":"user@xample.com",
"firstName":"Anthony",
"lastName":"Gonsalves",
"enabled":true,
"userCreated":"gtuser",
"userModified":"gtuser",
"createdTime":1422454697373,
"updatedTime":1422454697373
},
{
"userId":173,
"applicationId":"pgm-apn",
"username":"consumer.user",
"password":"password1",
"email":"test@egc.com",
"firstName":"sherlock ",
"lastName":"homes",
"enabled":true,
"userCreated":"gtuser",
"userModified":"gtuser",
"createdTime":1422010854246,
"updatedTime":1422010854246
}
我想正确显示日期datetime.Currently 它在 json data.Is 中显示为相同的字符串 data.Is 有任何方法可以在数据中转换它table
我在js中处理日期的时候总是用moment.js(http://momentjs.com/)
返回的日期值是 unix 时间戳,因此您需要对其进行转换。
这是一个示例 fiddle:http://jsfiddle.net/fws8u54g/
var created = 1422010854246;
moment.utc(created, "x").toISOString();
您可以使用 "render" 属性 来格式化您的列显示 http://datatables.net/reference/option/columns.render#function。
例如:
{
"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();
}
}
我已经使用 moment js 创建了演示,并使用渲染函数将 json 数据转换为所需格式。
还可以找到下面的代码:
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();
}
}]
});
对于可为空的日期时间 DateTime?,您需要使用不同的呈现函数:
$('#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();}
}
]};
对于dot.net和javascript,你可以像@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
},
{
"render": function (data) {
var d = new Date(data);
return d.toLocaleString();
}
要显示时间和日期,请添加以下代码:
"data": 'Date' ,
"render": function (data) {
var date = new Date(data);
var month = date.getMonth() + 1;
return (month.length > 1 ? month : "0" + month) + "/" + date.getDate() + "/" + date.getFullYear() + " " +(date.getHours() < 10 ? ("0"+date.getHours()) : date.getHours())+ ":"+(date.getMinutes() < 10 ? ("0"+date.getMinutes()) : date.getMinutes()) ;
//return date;
}
@Suraj Gulhane——你的答案是我唯一可以为 ASP.NET 核心服务器端 jQuery DataTables DateTime 格式工作的答案。
我对它进行了一些定制(基本上删除了时间,将年份放在第一位),但如果它对其他人有帮助,它看起来像这样:
{
"data": "dateOfBirth", "name": "Date Of Birth", "type": "date",
"render": function (data) {
var date = new Date(data);
var month = date.getMonth() + 1;
var day = date.getDate();
return date.getFullYear() + "/"
+ (month.length > 1 ? month : "0" + month) + "/"
+ ("0" + day).slice(-2);
//return date format like 2000/01/01;
}, "autoWidth": true
}
我正在尝试在 datatable 中显示一些数据,而我使用的 table 脚本是
$('#userData').dataTable({
"ajax": {
"url": "my-url",
"dataSrc": "",
},
"columns":[
{"data": "userId"},
{"data": "applicationId"},
{"data": "username"},
{"data": "firstName"},
{"data": "userCreated"},
{"data": "createdTime"},
{"data": "updatedTime"}
],
});
table 接收到的数据是 json,类似于
[
{
"userId":179,
"applicationId":"pgm-apn",
"username":"collaborator.user3",
"password":"password1",
"email":"user@xample.com",
"firstName":"Anthony",
"lastName":"Gonsalves",
"enabled":true,
"userCreated":"gtuser",
"userModified":"gtuser",
"createdTime":1422454697373,
"updatedTime":1422454697373
},
{
"userId":173,
"applicationId":"pgm-apn",
"username":"consumer.user",
"password":"password1",
"email":"test@egc.com",
"firstName":"sherlock ",
"lastName":"homes",
"enabled":true,
"userCreated":"gtuser",
"userModified":"gtuser",
"createdTime":1422010854246,
"updatedTime":1422010854246
}
我想正确显示日期datetime.Currently 它在 json data.Is 中显示为相同的字符串 data.Is 有任何方法可以在数据中转换它table
我在js中处理日期的时候总是用moment.js(http://momentjs.com/)
返回的日期值是 unix 时间戳,因此您需要对其进行转换。
这是一个示例 fiddle:http://jsfiddle.net/fws8u54g/
var created = 1422010854246;
moment.utc(created, "x").toISOString();
您可以使用 "render" 属性 来格式化您的列显示 http://datatables.net/reference/option/columns.render#function。
例如:
{
"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();
}
}
我已经使用 moment js 创建了演示,并使用渲染函数将 json 数据转换为所需格式。
还可以找到下面的代码:
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();
}
}]
});
对于可为空的日期时间 DateTime?,您需要使用不同的呈现函数:
$('#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();}
}
]};
对于dot.net和javascript,你可以像@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
},
{
"render": function (data) {
var d = new Date(data);
return d.toLocaleString();
}
要显示时间和日期,请添加以下代码:
"data": 'Date' ,
"render": function (data) {
var date = new Date(data);
var month = date.getMonth() + 1;
return (month.length > 1 ? month : "0" + month) + "/" + date.getDate() + "/" + date.getFullYear() + " " +(date.getHours() < 10 ? ("0"+date.getHours()) : date.getHours())+ ":"+(date.getMinutes() < 10 ? ("0"+date.getMinutes()) : date.getMinutes()) ;
//return date;
}
@Suraj Gulhane——你的答案是我唯一可以为 ASP.NET 核心服务器端 jQuery DataTables DateTime 格式工作的答案。
我对它进行了一些定制(基本上删除了时间,将年份放在第一位),但如果它对其他人有帮助,它看起来像这样:
{
"data": "dateOfBirth", "name": "Date Of Birth", "type": "date",
"render": function (data) {
var date = new Date(data);
var month = date.getMonth() + 1;
var day = date.getDate();
return date.getFullYear() + "/"
+ (month.length > 1 ? month : "0" + month) + "/"
+ ("0" + day).slice(-2);
//return date format like 2000/01/01;
}, "autoWidth": true
}