Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly deserialize a Json DateTimeOffset?

i got a problem while i try to deserialize a DateTimeOffset from a json. I saw a lot of questions here, but no one seems to work. I got from Json this dateTime : 05/04/2019 02:39:33 PM GMT and i want to keep the offset to zero. After the deserialization, by the way, i got my object with same exact time(In this case 02:39:33 PM) but with my time zone ( +02:00). I tried these two workaround, without success:

First of all, i tried to setup setting to my deserializer:

JsonSerializerSettings serializerSettings = new JsonSerializerSettings
    {
        DateFormatHandling = DateFormatHandling.IsoDateFormat,
        DateTimeZoneHandling = DateTimeZoneHandling.Utc,
        DateParseHandling = DateParseHandling.DateTimeOffset,
        DateFormatString = "dd/MM/yyyy hh:mm:ss tt 'GMT'"
    };

I tried this converter too:

class DateFormatConverter : IsoDateTimeConverter
{
    public DateFormatConverter(string format)
    {
        DateTimeFormat = format;
        DateTimeStyles = System.Globalization.DateTimeStyles.AssumeUniversal;
    }

So, i expected this reseult: 05/04/2019 02:39:33 PM +00:00 thanks to all that will answer me!

like image 293
Giuseppe Pennisi Avatar asked Aug 14 '26 13:08

Giuseppe Pennisi


1 Answers

Maybe change DateParseHandling.DateTimeOffset to DateParseHandling.None?