Skip to content

Latest commit

 

History

History
44 lines (39 loc) · 1.86 KB

SerializeDateTimeZoneHandling.md

File metadata and controls

44 lines (39 loc) · 1.86 KB

DateTimeZoneHandling setting

This sample uses the Argon.DateTimeZoneHandling setting to control how System.DateTime and System.DateTimeOffset are serialized.

public class Flight
{
    public string Destination { get; set; }
    public DateTime DepartureDate { get; set; }
    public DateTime DepartureDateUtc { get; set; }
    public DateTime DepartureDateLocal { get; set; }
    public TimeSpan Duration { get; set; }
}

snippet source | anchor

var flight = new Flight
{
    Destination = "Dubai",
    DepartureDate = new(2013, 1, 21, 0, 0, 0, DateTimeKind.Unspecified),
    DepartureDateUtc = new(2013, 1, 21, 0, 0, 0, DateTimeKind.Utc),
    DepartureDateLocal = new(2013, 1, 21, 0, 0, 0, DateTimeKind.Local),
    Duration = TimeSpan.FromHours(5.5)
};

var jsonWithRoundtripTimeZone = JsonConvert.SerializeObject(flight, Formatting.Indented, new JsonSerializerSettings());

Console.WriteLine(jsonWithRoundtripTimeZone);
// {
//   "Destination": "Dubai",
//   "DepartureDate": "2013-01-21T00:00:00",
//   "DepartureDateUtc": "2013-01-21T00:00:00Z",
//   "DepartureDateLocal": "2013-01-21T00:00:00+01:00",
//   "Duration": "05:30:00"
// }

snippet source | anchor