0

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

2
  • Not sure what you're asking here. Do you need help converting all those numbers in Date() to a readable date string? Commented Sep 13, 2012 at 21:07
  • @Gabriel Florit that's correct Commented Sep 14, 2012 at 5:40

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.