panjo
panjo

Reputation: 3515

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.

Time is DateTime property. I'm fetching this json objects in the view like following

$(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)/

What is the best way to convert this, on the server side or in the view and how to do this?

Thanks

Upvotes: 0

Views: 1185

Answers (1)

Yatrix
Yatrix

Reputation: 13775

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.

This is a solution I for doing it in JavaScript, found here: 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.

Upvotes: 1

Related Questions