question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deserialization of DateTimeOffset value fails depending on system's timezone

See original GitHub issue

I encountered an issue while materializing a date/time value as DateTimeOffset. Please see https://stackoverflow.com/questions/50628374 where I initially asked for help.

Source/destination types

public class RootObject
{
    [JsonProperty("revisedDate", NullValueHandling = NullValueHandling.Ignore)]
    public DateTimeOffset? RevisedDate { get; set; }
}

Source/destination JSON

{
  "revisedDate": "0001-01-01T00:00:00"
}

Expected behavior

In the abscence of a timezone, I expected Json.NET to in this case deserialize the value and return a valid DateTimeOffset equal to DateTimeOffset.MinValue.

Actual behavior

An exception was thrown in the DateTimeOffset.Parse method, and Json.NET reported “Could not convert string to DateTimeOffset: 0001-01-01T00:00:00. Path ‘resource.revisedDate’, line 20, position 44.” to the caller.

Steps to reproduce

Please refer to the answer provided by dbc for steps to reproduce the issue: https://stackoverflow.com/a/50631270/1503584

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:24
  • Comments:13

github_iconTop GitHub Comments

5reactions
a2741890commented, Feb 1, 2022

Still don’t understand why this issue still exists after nearly 3.5 years…

4reactions
zongbingwangcommented, Dec 12, 2018

I also met this issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Solved]-Json.NET deserializing DateTimeOffset value fails for ...
the workaround is to use isodatetimeconverter to deserialize your datetimeoffset properties with isodatetimeconverter.datetimestyles set to datetimestyles.
Read more >
C# – Json.NET deserializing DateTimeOffset value fails for ...
The workaround is to use IsoDateTimeConverter to deserialize your DateTimeOffset properties with IsoDateTimeConverter.DateTimeStyles set to DateTimeStyles.
Read more >
DateTime and DateTimeOffset support in System.Text.Json
The System.Text.Json library parses and writes DateTime and DateTimeOffset values according to the ISO 8601-1:2019 extended profile.
Read more >
Compare types related to date and time
The DateTimeOffset structure represents a date and time value, together with an offset that indicates how much that value differs from UTC.
Read more >
Getting Different value of time When Deserializing JSON ...
Hi. I'm trying to save selected time from lightning input type time into time fields of the object but at the time of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found