Destroy the date while keeping the local time

Json dates are tricky and the conversion seems to elude me. It seems that some of the time is lost inside the transformation.

I have the following Microsoft Json date returned from API. I know and can confirm the date May 5, 2017 7 am

enter image description here

However, when deserializing the date with newtonsoft, I can get it to store the timezone information. I've tried all different settings but can't seem to work it out.

enter image description here

My code for deserialization looks like this

var settings = new JsonSerializerSettings {
     DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
     DateParseHandling = DateParseHandling.DateTimeOffset,
     DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind
};

items = JsonConvert.DeserializeObject<List<UpcomingMeetingListDto>>(
                    responseContent, settings);

      

Of course it should be easy, I just can figure it out. I think it must be because the date format in json has no associated TZ information. Perhaps I need a custom date deserizer to handle this case or set the culture.

I am using Newtonsoft.Json version 9.0.1

+3


source to share


1 answer


DateTime is deserialized correctly but is in GMT. To display the local time, you must use the ToLocalTime () method.

For example, by adding a property to your upcoming MeetingListDto.



public DateTime LocalMeetingDate => MeetingDate.ToLocalTime();

      

Try the online epoch converter , it will show you both local time and gmt time.

+3


source







All Articles