Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a JSON date value via ASP.NET MVC using JSON.NET? [duplicate]

Possible Duplicate:
Format a Microsoft JSON date?

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

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

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

like image 620
ChrisP Avatar asked Aug 12 '09 00:08

ChrisP


People also ask

How to deserialize a JSON string?

A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.

How show JSON data table in HTML MVC?

Right click on Controller folder in the created MVC application, give the class name Home or as you wish and click OK. In the above controller class JsonResult method EmpDetails we have added the records into the Generic list and returning it as JSON to avoid database query for same result.

What is JSON converter?

A converter is a class that converts an object or a value to and from JSON. The System. Text. Json namespace has built-in converters for most primitive types that map to JavaScript primitives.


4 Answers

This was found in another post on Stack Overflow:

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.

like image 78
Jimbo Avatar answered Sep 18 '22 08:09

Jimbo


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

like image 37
Ryan Taylor Avatar answered Sep 22 '22 08:09

Ryan Taylor


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...

like image 22
Felix Avatar answered Sep 21 '22 08:09

Felix


Not everyone agrees with me that it's a good idea, but I find myself most often returning formatted strings instead of proper dates. See How I handle JSON dates returned by ASP.NET AJAX.

like image 36
Dave Ward Avatar answered Sep 18 '22 08:09

Dave Ward