TimeConverter

The TimeConverter class lets you serialize and deserialize a number of different types in both .NET and within the Skybrud.Essentials package. Currently the following types are supported:

The converter also supports a number of different standardized date and time formats:

You can decorate your properties with the JsonConverterAttribute class as shown below. The converter will use TimeFormat.Iso8601 by default, but you can specify another format as well - eg. TimeFormat.Rfc822:

public class Example {
    
    [JsonConverter(typeof(TimeConverter))]
    public DateTime Iso8601 { get; set; }
    
    [JsonConverter(typeof(TimeConverter), TimeFormat.Rfc822)]
    public DateTime Rfc822 { get; set;}
    
}

Unix time

To serialize to unix time, you could use TimeConverter and then specify TimeFormat.UnixTime as the second parameter for the attribute. But to make things a little easier, you could insetad use the UnixTimeConverter class directly as shown below:

public class Example {
    
    [JsonConverter(typeof(TimeConverter), TimeFormat.UnixTime)]
    public DateTime UnixTime1 { get; set; }
    
    [JsonConverter(typeof(UnixTimeConverter))]
    public DateTime UnixTime2 { get; set;}
    
}