Sai Avinash
Sai Avinash

Reputation: 4753

Difficulty with Conversion of Date Time to JSON data

I am trying to convert a list<T> into JSON data and then binding to Jqgrid.

The Problem is that list collection contains one of the column as DateTime.

When i see the resultant JSON response, the date time is not properly serialized. Its coming like this:

 /Date(1267408935000)/

How to convert list collection into JSON data properly when it contains date time columns.

Please help..

Upvotes: 1

Views: 979

Answers (3)

Oleg
Oleg

Reputation: 221997

If you use formatter: "date" for the corresponding column definition in colModel then jqGrid will make the corresponding conversion automatically. The problem could be only if you use some very old version of jqGrid (see documentation which described that the corresponding support was introduced starting with jqGrid 3.8.2).

Upvotes: 5

user2838643
user2838643

Reputation:

Create class with string property instead of DateTime.

public class MyJsonModel
{
   public string DateTimeString { get;set; }
}

var model = new MyJsonModel();
model.DateTimeString = DateTime.Now.ToString("MM-dd-yyyy"); //Any format you like

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500875

When you say it's "not properly serialized" - that looks okay to me. That's one format for JSON dates, coming from the Javascript Date constructor taking a "milliseconds since the Unix epoch" as a parameter. So the value you've given is March 1st 2010 02:02:15 UTC for example.

I'd expect the Javascript deserializing this data to be absolutely fine - but of course that's not necessarily the case... If it's really causing problems for you, read Scott Hanselman's blog post about JSON date/time formats - in particular, it explains how you can swap out the JSON formatter to be Json.NET, which uses an ISO-8601 format by default.

Upvotes: 2

Related Questions