简体   繁体   English

从json转换datetime对象

[英]converting datetime object from json

I have following code 我有以下代码

  var res = from c in model.Data 
            select new object[] { c.Id, c.Time, c.Name };

this res variable is sent as json object. 此res变量作为json对象发送。

Time is DateTime property. 时间是DateTime属性。 I'm fetching this json objects in the view like following 我在视图中获取此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" }
                    ]
        });
    });

Page is rendered with datetime fields like /Date(1346996934000)/ 页面使用日期时间字段(例如/Date(1346996934000)/

What is the best way to convert this, on the server side or in the view and how to do this? 在服务器端或视图中转换此内容的最佳方法是什么?如何执行此操作?

Thanks 谢谢

Is that number the date in ticks? 这个数字是日期的价格吗? If that's the case, then 如果是这样的话

DateTime date = new DateTime(long.Parse(1346996934000));

Found that here: Format from ticks to date 在这里发现: 从刻度到日期的格式

This would be converted to: Fri Sep 07 2012 01:48:54 in GMT-4 timezone. 它将转换为:GMT-4时区的Fri Sep 07 2012 01:48:54

This is a solution I for doing it in JavaScript, found here: http://deekshadev.blogspot.com/2011/03/convert-ticks-to-date-object.html 这是我使用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.

If that date is only needed for display, I wouldn't do it on the server level. 如果仅需要显示该日期,则不会在服务器级别执行该操作。 I'd convert on the client and display it as needed. 我会在客户端上转换并根据需要显示它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM