29 lines
950 B
C#
29 lines
950 B
C#
|
using System;
|
|||
|
using System.Text.Json;
|
|||
|
using System.Text.Json.Serialization;
|
|||
|
|
|||
|
namespace FirmTracker_Server.Utilities.Converters
|
|||
|
{
|
|||
|
public class DateTimeConverter : JsonConverter<DateTime>
|
|||
|
{
|
|||
|
private const string DateFormat = "yyyy-MM-ddTHH:mm";
|
|||
|
|
|||
|
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|||
|
{
|
|||
|
if (reader.TokenType == JsonTokenType.String)
|
|||
|
{
|
|||
|
if (DateTime.TryParseExact(reader.GetString(), DateFormat, null, System.Globalization.DateTimeStyles.None, out var date))
|
|||
|
{
|
|||
|
return date;
|
|||
|
}
|
|||
|
}
|
|||
|
throw new JsonException("Invalid date format");
|
|||
|
}
|
|||
|
|
|||
|
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
|
|||
|
{
|
|||
|
writer.WriteStringValue(value.ToString(DateFormat));
|
|||
|
}
|
|||
|
}
|
|||
|
}
|