8

When I return object that contains DateTime property using

return Json(value); 

on client I receive

"/Date(1336618438854)/" 

If i return the same value using

return Json(JsonConvert.SerializeObject(value)); 

then the returned serialized value (together with serialized object) is time zone aware:

"/Date(1336618438854-0400)/" 

Is there any way to get consistent DateTime result without without double serialization? I read somewhere that MS will include Newtonsoft JSON into MVC?

2
  • 1
    Hmm Google? That sounds familiar. Newton's serialization is working fine. Question is why MVC is ignoring time zone when they have adopted /Date()/ format. Commented May 10, 2012 at 14:07
  • Simply using $.parseJSON(result) will properly parse the dates. stackoverflow.com/a/4540007/752974 Commented Dec 30, 2014 at 20:36

4 Answers 4

11

I finally figured out what to do.
I will switch my project to ISO 8601 DateTime format. Serialization is done nicely with JSON.net, just by decorating the datetime property on the object with JsonConverter attribute.

 public class ComplexObject { [JsonProperty] public string ModifiedBy { get; set; } [JsonProperty] [JsonConverter(typeof(IsoDateTimeConverter))] public DateTime Modified { get; set; } ... } 

To return serialized object to the client ajax call I can do:

 return Json(JsonConvert.SerializeObject(complexObjectInstance)); 

and on the client:

 jsObject = JSON.parse(result) 

Now I am thinking it would be probably simple to override default ASP.NET MVC default JSON serializer to us Newtonsoft JSON.net ISO 8601 serialization, and yes principle should be similar to this thread: Change Default JSON Serializer Used In ASP MVC3.

Sign up to request clarification or add additional context in comments.

1 Comment

Great answer but I found that the Newtonsoft.Json attributes are not necessary. Simply using $.parseJSON(result) will properly parse the dates. stackoverflow.com/a/4540007/752974
0

In the WebApiConfig set:

config.Formatters.Remove(config.Formatters.XmlFormatter); //config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html")); config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json")); config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented; config.Formatters.JsonFormatter.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat; config.MapHttpAttributeRoutes(); 

In the ApiController return this:

return Request.CreateResponse(HttpStatusCode.OK, obj); 

Good Luck CAhumada

Comments

-2

If you dont want to dig in to the Parsing thing than simply convert your date in to the string than parse it with the JSON.

for example

return Json(DateTime.Now.ToString("your date format if you want to specify")); 

2 Comments

Sorry, but this post is not about showing dates. It is about consistency in serialized date format for complex objects containing DateTime properties in server to client and client to server scenarios. And what you are suggesting I am doing in the 2nd line with SerializeObject for the whole object.
@user1188755 ... check below this stackoverflow.com/questions/668488/…
-2

It returns Server Date Format. You need to define your own function.

function jsonDateFormat(jsonDate) { // Changed data format; return (new Date(parseInt(jsonDate.substr(6)))).format("mm-dd-yyyy / h:MM tt"); 

};

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.