Question

I have a MVC3 project that manages events' dates. The problem is that when I write a JsonResult

   //...
   var StartDate=new DateTime(1999,12,10,2,3,40);
   return Json(StartDate,JsonBehavior.AllowGet);

the response's body format is something similar to

"StartDate":"\/Date(1374962232247)\/"

This result is giving me a date in the last day according to the server timezone, Instead of returning something like

 "StartDate":"\/Date(1374962232247-0600)\/"

which contains some server timezone info! I can't follow the SHanselman's post(like it but It seems not appropriated)! So, can someone tell me how to let Newtonsoft.Json.5.0.6 packages convert Json DateTime with Timezone info? Kind Regards

Was it helpful?

Solution

I think this link should help you with setting up MVC3 project working with Newtonsoft Json Serializer. For ISO date time formatter you should change the following lines in the JsonNetResult constructor.

public JsonNetResult()
{
    Formatting = Formatting.None;
    var settings = new JsonSerializerSettings();
    settings.Converters.Add(new IsoDateTimeConverter());
    SerializerSettings = settings;
    JsonRequestBehavior = JsonRequestBehavior.DenyGet;
}

You have to derive your controllers from BaseController to use the custom result.

hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top