2

My code base had been using JavaScriptSerializer for long time, now I am planning to migrate to Json.Net. I saw that DateTime Serializing on both work differently.

Console.WriteLine(new JavaScriptSerializer().Serialize(DateTimeOffset.UtcNow));

Output:"\/Date(1403629024695)\/"

Console.WriteLine(JsonConvert.SerializeObject(DateTimeOffset.UtcNow));

Output: "2014-06-24T16:57:04.6954145+00:00"

Is there a way to get Json.net to serialize similar to JavaScriptSerializer?

1 Answer 1

3

You can use DateFormatHandling.MicrosoftDateFormat

Console.WriteLine(JsonConvert.SerializeObject(DateTimeOffset.UtcNow, 
                                              new JsonSerializerSettings() { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat }));
Sign up to request clarification or add additional context in comments.

1 Comment

Format is different. returns: /Date(1667894400000-0800)/ instead of /Date(1403629024695)/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.