Utf8jsonreader Datetimeoffset Parsing Rfc 3339 Jun 2026

if (reader.TokenType == JsonTokenType.PropertyName)

string? dateString = reader.GetString();

return dto;

Console.WriteLine($"Parsing failed: ex.Message");

if (DateTimeOffset.TryParse(dateStr, CultureInfo.InvariantCulture, out var dto)) return dto; utf8jsonreader datetimeoffset parsing rfc 3339

: RFC 3339 allows a space to separate the date and time, but Utf8JsonReader strictly requires the "T" separator .

// Fallback to standard RFC 3339 return reader.GetDateTimeOffset(); if (reader

ReadOnlySpan<byte> utf8Span = reader.ValueSpan; Span<char> buffer = stackalloc char[utf8Span.Length]; int chars = Encoding.UTF8.GetChars(utf8Span, buffer); ReadOnlySpan<char> dateStr = buffer.Slice(0, chars);

Because Utf8JsonReader is strict about uppercase characters and separators, you may encounter valid RFC 3339 strings that it cannot parse directly. In these cases, it is common practice to implement a using DateTimeOffset.Parse after extracting the string. Console.WriteLine($"Parsing failed: ex.Message")

: Use reader.TryGetDateTimeOffset(out var value) to attempt parsing directly from the buffer.