18

Possible Duplicate:
how to force netwtonsoft json serializer to serialize datetime property to string?

I am using Newtonsoft.Json to convert my object in to JSON file. But I am having issue with the DateTime, in my object datetime field is set as "7/30/2012 8:29:12 PM" but in JSON file. I am getting DateTime field in this format:

"\/Date(1343660352227+0530)\/".

I need to get the DateTime in the same format as that of the object. Is it possible? What should I do in order to get the same format?

Ihor Patsian
  • 1,288
  • 2
  • 15
  • 25
Amol Kolekar
  • 2,307
  • 5
  • 30
  • 45
  • 3
    You are not the only one with that problem, see [Scott Hanselman - On the nightmare that is JSON Dates. Plus, JSON.NET and ASP.NET Web API](http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx) – M. Mennan Kara Aug 01 '12 at 10:59
  • thanks for the reply,i will check the links... – Amol Kolekar Aug 01 '12 at 11:17

1 Answers1

16

I got the solution as follow.

JsonConvert.SerializeObject(this, Formatting.None, new IsoDateTimeConverter() {
    DateTimeFormat = "yyyy-MM-dd hh:mm:ss"
});

Here is the answer:

how to force netwtonsoft json serializer to serialize datetime property to string?

Narendra Jadhav
  • 10,052
  • 15
  • 33
  • 44
Amol Kolekar
  • 2,307
  • 5
  • 30
  • 45