OrchardCMS/OrchardCore · error · JsonException
Unable to convert " " to DateTime.
Error message
Unable to convert "{stringValue}" to DateTime. What it means
The JSON token could not be parsed as a DateTime: Utf8JsonReader.TryGetDateTime failed (token is not an ISO 8601 string) and the fallback DateTime.TryParse on the raw string also failed. This fires when the JSON contains a date in a non-ISO format (e.g. 'dd/MM/yyyy') or a non-date value where a DateTime is expected.
Solutions
- Serialize dates as ISO 8601 strings (yyyy-MM-ddTHH:mm:ssZ), the format this converter writes
- Add a CultureInfo/DateTimeStyles-specific parse branch to the converter for the legacy format in use
- Correct the producing side to emit a DateTime-compatible string instead of an arbitrary format
Defensive patterns
Strategy: fallback
When it happens
Trigger: Thrown at src/OrchardCore/OrchardCore.Abstractions/Json/Serialization/DateTimeJsonConverter.cs:26 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13).
Data as JSON: /api/errors/52087390caeb98d1.
Report an issue: GitHub.
Appendix: source
Thrown at src/OrchardCore/OrchardCore.Abstractions/Json/Serialization/DateTimeJsonConverter.cs:26
{
public static readonly DateTimeJsonConverter Instance = new();
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (typeToConvert != typeof(DateTime))
{
throw new ArgumentException("Unexpected type to convert.", nameof(typeToConvert));
}
if (!reader.TryGetDateTime(out var value))
{
var stringValue = reader.GetString();
if (DateTime.TryParse(stringValue, out value))
{
return value;
}
throw new JsonException($"Unable to convert \"{stringValue}\" to DateTime.");
}
return value;
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture));
}
View on GitHub (pinned to 4306c0717f)