41

I am using Newtonsoft's Json when i serialze a date time property i get the json response as:

..."CreatedOn":"\/Date(1317303882420+0500)\/",...

i want it to be in simple string as

..."createdOn": "2011-05-05 14:03:07", ...

while my class property is DateTime, how can i force to serialze it as string, as we can add attribute to change the property name as

  [JsonProperty("id")]
        public int ProductID { get; set; }

is there a similar way to force a DateTime property to serialize to string??

vakas
  • 1,799
  • 5
  • 23
  • 40
  • 1
    Please provide the JSON object that you are trying to deserialize. – Frank Oct 06 '11 at 14:08
  • 1
    Please note that as of Json.NET 4.5 Dates are written using the ISO 8601 format by default http://james.newtonking.com/json/help/index.html?topic=html/DatesInJSON.htm (ISO 8601 formatted string: "2009-02-15T00:00:00Z") – Aaron Hoffman May 17 '14 at 15:24

1 Answers1

66

From a post made by James Newton-King on StackOverflow, it looks like you can do this.

string isoJson = JsonConvert.SerializeObject(this, new IsoDateTimeConverter());
// {"Details":"Application started.","LogDate":"2009-02-15T00:00:00Z"}    

Referenced answer: Parsing JSON DateTime from Newtonsoft's JSON Serializer

Also here is the documentation on Json.NET and dates: Serializing Dates in JSON

Here is an example of using the DateTimeFormat property to customize the output:

return JsonConvert.SerializeObject(this, Formatting.None, new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" });
Community
  • 1
  • 1
James Roland
  • 8,183
  • 4
  • 30
  • 16
  • 2
    thanx for the response ... this is cool it works.. but infact i need the date time to be as "LogDate":"2009-02-15 00:00:00" not T in between date and time – vakas Oct 06 '11 at 19:08
  • Have a look at the documentation in the last link in the post. It says something about: "The IsoDateTimeConverter class has a property, DateTimeFormat, to further customize the formatted string." Try fiddling with that. – James Roland Oct 06 '11 at 19:56
  • 5
    is there a way to decorate my MVC model class property so Newtonsoft.Json will utilize a preferred DateTime format? Or can a converter be specified at a global level so that my whole WebApi stable of controllers serialize all models with Date properties using a desired date format? – bkwdesign Oct 23 '13 at 20:10
  • 7
    ok. figured it out just now: WebApiConfig.Register method, added this line: GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.DateFormatString = "dd/MM/yyyy" – bkwdesign Oct 23 '13 at 20:19
  • Shouldn't that be `HH:mm:ss` on the second example? – Adrian Wragg Mar 13 '14 at 14:41
  • 2
    Yes it should be HH because there is no tt to indicate AM or PM. I tried to edit to fix it but there is a BS rule that only edits of six characters or more are accepted so I didn't waste my time trying to think of 4 more characters to edit. – Aaron Axvig Mar 18 '14 at 17:17
  • And for this `2018-02-06T14:40:43.511Z` ? – Kiquenet Mar 20 '18 at 13:21
  • `settings.Converters.Add(new Newtonsoft.Json.Converters.IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.FFFZ" });` – Kiquenet Mar 20 '18 at 14:57