views:

1600

answers:

6

ASP.NET Json() formats and returns a date as

{"d":"\/Date(1240718400000)\/"}

which has to be dealt w/ on the client side which is problematic. What are your suggestions for approaches to sending date values back and forth?

Thanks

A: 

Take a look at this link:

http://schotime.net/blog/index.php/2008/06/19/jquery-ajax-aspnet-and-dates/

It details how to work with ASP.NET MVC&jQuery pass dates via JSON between server and client side.

Alex
This provides some insights, but deals w/ jQuery to ASP.NET web service rather than an MVC controller returning JsonResult via Json(). I am interested in jQuery to ASP.NET MVC w/o MSFT ajax.
ChrisP
The problem is primarily w/ complex objects that have a date property rather than a single date value.
ChrisP
The JSON format is the exact same in the example to what you described. Date Property or Single Date Value does not make a difference.
Alex
+2  A: 

Not everyone agrees with me that it's a good idea, but I find myself most often returning formatted strings instead of proper dates: http://encosia.com/2009/04/27/how-i-handle-json-dates-returned-by-aspnet-ajax/

Dave Ward
+2  A: 

If you are not tied to the MS JSON serializer you could use Json.NET. It comes with an IsoDateTimeConverter to handle issues with serializing dates. This will serialize dates into an ISO 8601 formatted string.

For instance, in our project serializing myObject is handled via the following code.

JsonNetResult jsonNetResult = new JsonNetResult();
jsonNetResult.Formatting = Formatting.Indented;
jsonNetResult.SerializerSettings.Converters.Add(new IsoDateTimeConverter());
jsonNetResult.Data = myObject;

If you decide to take the Json.NET plunge you'll also want to grab JsonNetResult as it returns an ActionResult that can be used in ASP.NET MVC application. It's quite easy to use.

For more info see: Good (Date)Times with Json.NET

Ryan Taylor
Don't know how we missed this, thanks
Marc
+3  A: 

It may be ugly, but it works:

 var epoch = (new RegExp('/Date\\((-?[0-9]+)\\)/')).exec(d);
 $("#field").text((new Date(parseInt(epoch[1]))).toDateString());

Probably, it is not necessary to match the whole string, and just (-?[0-9]+) is enough...

Felix
A: 

After playing with the JsonNet library, I'm wondering why you would choose to use the IsoDateTimeConverter over the JavascriptDateTimeConverter.

I found this to be easier to use with the ExtJS interfaces that I was using when serializing dates from the an MVC Controller.

JsonNetResult jsonNetResult = new JsonNetResult();
jsonNetResult.Formatting = Formatting.Indented;
jsonNetResult.SerializerSettings.Converters.Add(new JavaScriptDateTimeConverter());
jsonNetResult.Data = myObject;

I'm getting this data back into an Ext.data.JsonStore which is able to get the returned value as a date without me having to specify a date format to parse with.

        store:new Ext.data.JsonStore({
            url: pathContext + '/Subject.mvc/Notices',
            baseParams: { subjectId: this.subjectId },
            fields: [
               {name: 'Title'},
               {name: 'DateCreated', type: 'date' }
            ]
        }),

The Json returned looks like this:

[{"Title":"Some title","DateCreated":new Date(1259175818323)}]

Any reason to convert to ISO 8601 format and back if you don't have to?

Thanks,

CBrown

brow-cow
A: 

This found at another post on stackoverflow:

var date = new Date(parseInt(jsonDate.substr(6))); 

The substr function takes out the "\/Date(" part, and the parseInt function gets the integer and ignores the ")\/" at the end. The resulting number is passed into the Date constructor

Jimbo