0

i am really new to Json. I have a datetime (Extracted date) in an object and which has the following value

2018-10-10 10:50:00 

I am using this specific function from Json.net

JsonConvert.SerializeObject(object)

The returned Json String has letter T in the extracted_date i.e.,

{
    "extracted_date": "2018-10-10T10:50:00"
}

After research I discovered that it could be to do with ISO Format which Json.net converts the date to. How do I not include the letter in the extracted date? Or is it not possible?

I am using VB.Net and used the following code as per one of the comments and still have the same issue

JSONString = JsonConvert.SerializeObject(newJob, Formatting.Indented,
                                                            New JsonSerializerSettings With
                                                            {.DateFormatHandling = DateFormatHandling.IsoDateFormat})
Abe
  • 1,879
  • 2
  • 24
  • 39
  • https://stackoverflow.com/a/14252634/3201354 – Jignesh Oct 10 '18 at 10:21
  • See [Serializing dates in Json.Net](https://www.newtonsoft.com/json/help/html/DatesInJSON.htm) - if none of the default options do what you need, that shows how to write a custom converter which gives you complete control over the output. – stuartd Oct 10 '18 at 10:30

2 Answers2

1

DateFormatString
Gets or sets how DateTime and DateTimeOffset values are formatted when writing JSON text, and the expected date format when reading JSON text. The default value is "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK".

DateParseHandling Gets or sets how date formatted strings, e.g. "/Date(1198908717056)/" and "2012-03-21T05:40Z", are parsed when reading JSON. The default value is DateTime.

IsoDateFormat Dates are written in the ISO 8601 format, e.g. "2012-03-21T05:40Z".

MicrosoftDateFormat Dates are written in the Microsoft JSON format, e.g. "/Date(1198908717056)/".

look here

 JsonConvert.SerializeObject([your object], Formatting.Indented,
                            new JsonSerializerSettings
                            {
                             DateFormatHandling = DateFormatHandling.IsoDateFormat
                            });
go..
  • 958
  • 7
  • 15
  • 1
    While that will probably work, the explanation to set indenting is confusing because it should work with the settings, not the indentation. – nvoigt Oct 10 '18 at 10:24
  • @sinan Baran Thanks for your suggestion, I have tried and still have the issue what am i missing, i am using vb.net by the way. I have updated my question accordingly. – Abe Oct 10 '18 at 10:45
0

Based on the one of the comments I have got it working. My code looks like this, hope this will help someone in future.

Dim JSONString = JsonConvert.SerializeObject(newJob, Formatting.Indented,
                                                            New JsonSerializerSettings With
                                                            {.DateFormatHandling = DateFormatHandling.IsoDateFormat,
                                                            .DateFormatString = "yyyy-MM-dd hh:mm:ss"
                                                            })
Abe
  • 1,879
  • 2
  • 24
  • 39