我有以下代码
var res = from c in model.Data
select new object[] { c.Id, c.Time, c.Name };
这个res变量作为json对象发送。
时间是DateTime属性。 我正在视图中获取此json对象,如下所示
$(document).ready(function () {
$('#myDataTable').dataTable({
"bServerSide": true,
"sAjaxSource": "/Home/AjaxHandler",
"bProcessing": true,
"aoColumns": [
{ "sName": "ID",
"bSearchable": false,
"bSortable": false,
"fnRender": function (oObj) {
return '<a href=\"Details/' +
oObj.aData[0] + '\">View</a>';
}
},
{ "sName": "Time" },
{ "sName": "Name" }
]
});
});
使用/Date(1346996934000)/
在服务器端或视图中转换它的最佳方法是什么?如何执行此操作?
由于
答案 0 :(得分:1)
这个数字是刻度中的日期吗?如果是这样的话,那么
DateTime date = new DateTime(long.Parse(1346996934000));
在此处找到:Format from ticks to date
这将在GMT-4时区转换为: Fri Sep 07 2012 01:48:54 。
这是我在JavaScript中执行此操作的解决方案,可在此处找到:http://deekshadev.blogspot.com/2011/03/convert-ticks-to-date-object.html
//convert the event day to a date object
var startticks = new Date(ticks * 1000);
//convert today to ticks will be in milliseconds
var todayticks = new Date().getTime();
var diff = startticks - todayticks;
var days = Math.floor(diff/(24*60*60*1000));
var hours = (diff/(60*60*1000)) % 24;
var mins = (diff/(60*1000)) % 60;
Here ticks was in seconds, so multiplying it with 1000 to convert to milliseconds.
如果仅显示该日期,我不会在服务器级别上执行此操作。我会在客户端转换并根据需要显示它。