2

This sample code

DateTime date = new DateTime(2020, 01, 27);
string str = Newtonsoft.Json.JsonConvert.SerializeObject(date, Newtonsoft.Json.Formatting.None,
    new Newtonsoft.Json.JsonSerializerSettings() {
         DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat });

returns str = "\"2020-01-27T00:00:00\"" - so the string's content is surrounded with quotes (here written in escaped form as the debugger does).

But I want only the date and time in ISO format. Why are there quotes? How can I avoid them without removing them afterwards?

I already looked for all serializer options, but didn't found the one helping me...

Nick
  • 472
  • 3
  • 18
  • 4
    Because Json.NET is a JSON serializer but JSON [has no primitive format for dates & times](https://www.newtonsoft.com/json/help/html/DatesInJSON.htm) so serializes a `DateTime` as a JSON string primitive, which is quoted as required by the [JSON standard](https://www.json.org/json-en.html). – dbc Jan 27 '20 at 16:56
  • 1
    The quotes are to satisfy the [json syntax](https://www.w3schools.com/js/js_json_syntax.asp) for a string, as there is no global datetime object like there is for an integer for example. – Jesse Jan 27 '20 at 16:57
  • you have to format datetime. https://www.c-sharpcorner.com/blogs/date-and-time-format-in-c-sharp-programming1 – LinkedListT Jan 27 '20 at 16:57
  • The [dotnetfiddle.com example for NewtonSoft](https://dotnetfiddle.net/Mobile?id=5VR5rs#code-editor) doesn't seem to have this problem.. – Caius Jard Jan 27 '20 at 17:03
  • 1
    Do these comments answer your question, or do you still need some help? What problem are you trying to solve? [Given a DateTime object, how do I get an ISO 8601 date in string format?](https://stackoverflow.com/q/114983/3744182)? – dbc Jan 27 '20 at 17:11
  • Thank you all - I think I got it now. Json formatting works perfect as long as you create a complete json, but creating only single fields is problematic because this would not satisfy the json rules. – Nick Jan 27 '20 at 17:12
  • I wanted to avoid defining the date format as a string - json formatting creates a perfect ISO date. Is there a standard for the DateTime.ToString() function, so I can refer to this ISO format and not needing to write date.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffff")? – Nick Jan 27 '20 at 17:15
  • 1
    [this answer](https://stackoverflow.com/a/115034/3744182) to [Given a DateTime object, how do I get an ISO 8601 date in string format?](https://stackoverflow.com/q/114983/3744182) seems to show what you want, see https://dotnetfiddle.net/AMKLKo. Or you could just add a const string somewhere: `public class Constants { public const string IsoDateTimeFormat = "yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffff"; }`. – dbc Jan 27 '20 at 17:30
  • Thank you so much! I'm now using date.ToString("s") for ISO 8601 format and that's it! Solved all my issues (for this question...)! – Nick Jan 27 '20 at 17:34

1 Answers1

1

If you look up the Specification of Json, you would notice that it doesn't contain a proper definition of DateTime.

This is the reason, by default, Json.Net represents DateTime as string using the ISO norms representation. Having said so, one alternative representation would be using JavaScriptDateTimeConverter.

This isn't exactly ISO norm, and not even valid JSON according to specification(given in link above), but Json.Net does support it. For example,

var  data = new Person{Name= "ABC", Dob= new DateTime(2020, 01, 27)};
string str = Newtonsoft.Json.JsonConvert.SerializeObject(data,new JavaScriptDateTimeConverter());

Output

{"Name":"ABC","Dob":new Date(1580063400000)}

Please note that, you would need to use the JavaScriptDateTimeConverter for Deserializing as well.

var dt = JsonConvert.DeserializeObject<Person>(str,new JavaScriptDateTimeConverter());
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51