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:

  • DateTime
  • DateTimeOffset
  • EssentialsDateTime
  • EssentialsTime
  • EssentialsDate (no time, only date)
  • EssentialsPartialDate (no time, only date)

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

  • TimeFormat.Iso8601
    Indicates that the format is ISO 8601.
  • TimeFormat.Rfc822
    Indicates that the format is RFC 822.
  • TimeFormat.Rfc2822
    Indicates that the format is RFC 2822.
  • TimeFormat.UnixTime
    Indicates that the format is Unix time.

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;}
    
}