0

Please help with the following date format, how can i convert this to the correct date time?

2019-10-22T02:00:00-04:00

Not sure what will be used to convert the datetime.

I just passed it to the variable because it is get from a json.

var parse = JObject.Parse(times);

var dateTime = parse["Date"]["overTime"];

It returns a 23/10/2019 2:00:00 am why is that the date on the 23 but the supposedly be on the 22. How can I return the right date and time. Please can you show me how to convert the date and explain the conversion.

Rother Jon
  • 33
  • 1
  • 4
  • What time zone are you in? – Sweeper Oct 27 '19 at 13:33
  • How are you printing `dateTime`? I'm assuming `Console.WriteLine(dateTime.Value())`? – Sweeper Oct 27 '19 at 13:36
  • @Sweeper Philippine Standard Time GMT+8 but I don't want it to be converted to my timezone. I has to be the same timezone as the given date for it to be correct? – Rother Jon Oct 27 '19 at 13:37
  • @Sweeper Yes I am printing in just Console.WriteLine(dateTime); – Rother Jon Oct 27 '19 at 13:39
  • The original timezone offset of date that is parsed *does not* become an intrinsic property of the DateTime object. You would have to separately parse the "-04:00" offset, and format the DateTime output with that offset, to ensure that the original values for date and hour come out the same. – kshetline Oct 27 '19 at 13:43
  • Can you edit your question to include the JSON string `times` you are trying to parse? – Brian Rogers Oct 27 '19 at 14:18
  • You need to parse your JSON with `DateParseHandling.None` or `DateParseHandling.DateTimeOffset`. see: [JToken: Get raw/original JSON value](https://stackoverflow.com/a/35141787/3744182) and [Json.Net messes up timezones for DateTimeOffset when serializing](https://stackoverflow.com/a/44430768/3744182) and – dbc Oct 27 '19 at 15:45

2 Answers2

1

Instead of using JObject.Parse to parse the date/time, use DateTimeOffset.Parse. That will retain the original timezone offset, which the other form of parsing does not.

kshetline
  • 12,547
  • 4
  • 37
  • 73
  • Will the DateTimeOffset.Parse affect the other data in the json file or only the dates? – Rother Jon Oct 27 '19 at 14:00
  • This is a little more complicated than I realized when I first looked at it. I was thinking of `times` as just the string "2019-10-22T02:00:00-04:00", but now I see that it's probably a larger JSON value that includes multiple pieces of data. I think the problem is that `JObject.Parse` is automatically converting any string that looks like a date into a `DateTime` object for you, before you'd have a chance to parse it differently. You need a different parsing method, that leaves the date/time strings alone, keeping them as strings, so you can then use `DateTimeOffset.Parse` after. – kshetline Oct 27 '19 at 14:12
1

I have solved the problem by using following code below

JObject example = JsonConvert.DeserializeObject<JObject>(times, new JsonSerializerSettings { DateParseHandling = DateParseHandling.None });

Thanks for your answers!

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Rother Jon
  • 33
  • 1
  • 4