Scarlet.System.Text.Json.DateTimeConverter 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Scarlet.System.Text.Json.DateTimeConverter --version 1.0.0                
NuGet\Install-Package Scarlet.System.Text.Json.DateTimeConverter -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Scarlet.System.Text.Json.DateTimeConverter" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Scarlet.System.Text.Json.DateTimeConverter --version 1.0.0                
#r "nuget: Scarlet.System.Text.Json.DateTimeConverter, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Scarlet.System.Text.Json.DateTimeConverter as a Cake Addin
#addin nuget:?package=Scarlet.System.Text.Json.DateTimeConverter&version=1.0.0

// Install Scarlet.System.Text.Json.DateTimeConverter as a Cake Tool
#tool nuget:?package=Scarlet.System.Text.Json.DateTimeConverter&version=1.0.0                

Overview

The JsonDateTimeConverterAttribute allows you to specify a custom date format for DateTime, DateTimeOffset, and their nullable counterparts when serializing and deserializing JSON using System.Text.Json. This ensures consistency in how date and time values are handled across your application.

Installation

Ensure you have the necessary .NET target framework installed. This attribute is compatible with:

  • .NET 6
  • .NET 7
  • .NET 8

Usage

Example Model

using System;
using System.Text.Json.Serialization;
using Scarlet.System.Text.Json.DateTimeConverter;

public class MyModel
{
    [JsonDateTimeConverter("yyyy-MM-dd")]
    public DateTime Date { get; set; }

    [JsonDateTimeConverter("yyyy-MM-ddTHH:mm:ss.fffZ")]
    public DateTimeOffset DateTimeOffset { get; set; }
}

Example Program

using System;
using System.Text.Json;

public class Program
{
    public static void Main()
    {
        var model = new MyModel
        {
            Date = DateTime.Now,
            DateTimeOffset = DateTimeOffset.Now
        };

        // Serialize
        string jsonString = JsonSerializer.Serialize(model);
        Console.WriteLine($"Serialized JSON: {jsonString}");

        // Deserialize
        var deserializedModel = JsonSerializer.Deserialize<MyModel>(jsonString);
        Console.WriteLine($"Deserialized Date: {deserializedModel.Date}");
        Console.WriteLine($"Deserialized DateTimeOffset: {deserializedModel.DateTimeOffset}");
    }
}

Notes

  • The JsonDateTimeConverterAttribute can be applied to properties of type DateTime, DateTime?, DateTimeOffset, and DateTimeOffset?.
  • The format string provided to the attribute should follow the standard date and time format strings in .NET.
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0 82 10/29/2024
1.0.0 72 10/28/2024