JSON date format problem in ASP.NET MVC

Posted by Sheonarayan under Error and Solution on 12/13/2015 | Points: 10 | Views : 740 | Status : [Administrator] | Replies : 0

Problem


While returning a JSON string from controller action method to the view, the date and time field gets converted into numeric digit like this.

"{\"AutoId\":16,\"FirstName\":\"Sheo\",\"LastName\":\"Narayan\",\"Age\":50,\"Active\":true,\"BirthDate\":\"\\/Date(1450015663151)\\/\"}"


Easiest Solution

The easiest solution of this problem is to use NewtonSoft to serialize to json string instead of using inbuilt ASP.NET MVC

// return Json(model, JsonRequestBehavior.AllowGet); // do not use this
return Json(Newtonsoft.Json.JsonConvert.SerializeObject(model), JsonRequestBehavior.AllowGet); // use this

Using NewtonSoft JsonConvert would change the date to readable format like below

"{\"AutoId\":16,\"FirstName\":\"Sheo\",\"LastName\":\"Narayan\",\"Age\":50,\"Active\":true,\"BirthDate\":\"2015-12-13T20:17:09.3750692+05:30\"}"


Will write a complete article soon on this on how to change even the date format and how to change the json property names to camelCase.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com



Responses

(No response found.)

Login to post response