{"record":{"id":"c0ef16fef33d240d","repo":"OrchardCMS/OrchardCore","slug":"cannot-convert-value-to-datetimeoffset","errorCode":null,"errorMessage":"Cannot convert {value} to DateTimeOffset","messagePattern":"Cannot convert (.+?) to DateTimeOffset","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs","lineNumber":318,"sourceCode":"    {\n        return value?._jsonValue?.GetValue<char?>();\n    }\n\n    public static explicit operator DateTime(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<DateTime>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to DateTime\");\n    }\n\n    public static explicit operator DateTime?(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<DateTime?>();\n    }\n\n    public static explicit operator DateTimeOffset(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<DateTimeOffset>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to DateTimeOffset\");\n    }\n\n    public static explicit operator DateTimeOffset?(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<DateTimeOffset?>();\n    }\n\n    public static explicit operator decimal(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<decimal>()\n            ?? throw new InvalidCastException($\"Cannot convert {value} to Decimal\");\n    }\n\n    public static explicit operator decimal?(JsonDynamicValue value)\n    {\n        return value?._jsonValue?.GetValue<decimal?>();\n    }\n","sourceCodeStart":300,"sourceCodeEnd":336,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs#L300-L336","documentation":"JsonDynamicValue's explicit operator DateTimeOffset converts the wrapped JSON value via GetValue<DateTimeOffset>() and throws InvalidCastException when the result is null or the JSON token cannot be represented as a DateTimeOffset. The non-nullable operator deliberately fails fast; the nullable sibling returns null instead.","triggerScenarios":"Executing `(DateTimeOffset)jsonDynamicValue` where the wrapper or its _jsonValue is null, or the JSON token is a string lacking a timezone-offset-parseable value, a number, boolean, object, or array.","commonSituations":"Parsing timestamps from JSON payloads where the value is stored as a plain string without offset info or is null; deserializing API/document JSON with dynamic access; timezone-aware fields that were saved as empty strings.","solutions":["Ensure the JSON value is a parseable DateTimeOffset string (e.g. with offset like 2024-01-01T00:00:00+00:00)","Use the nullable cast (DateTimeOffset?) and handle null instead of throwing","Pre-parse with DateTimeOffset.TryParse/DateTimeOffset.Parse on value.ToString() when the format is uncertain","Null-check the JsonDynamicValue wrapper before casting"],"exampleFix":"// before\nvar when = (DateTimeOffset)jsonDynamicValue[\"Timestamp\"];\n// after\nvar when = (DateTimeOffset?)jsonDynamicValue[\"Timestamp\"] ?? DateTimeOffset.UtcNow;","handlingStrategy":"type-guard","validationCode":"if (jsonDynamicValue is null) throw new InvalidOperationException(\"null wrapper\");\nvar raw = jsonDynamicValue.ToString();\nif (!DateTimeOffset.TryParse(raw, CultureInfo.InvariantCulture, DateTimeStyles.None, out _))\n    throw new InvalidOperationException($\"'{raw}' is not a DateTimeOffset\");\nvar dto = (DateTimeOffset)jsonDynamicValue;","typeGuard":"static bool TryGetDateTimeOffset(JsonDynamicValue v, out DateTimeOffset value)\n{\n    value = default;\n    if (v is null) return false;\n    var dto = (DateTimeOffset?)v;\n    if (dto is null) return false;\n    value = dto.Value;\n    return true;\n}","tryCatchPattern":"DateTimeOffset dto;\ntry\n{\n    dto = (DateTimeOffset)jsonDynamicValue[\"Timestamp\"];\n}\ncatch (InvalidCastException ex)\n{\n    dto = DateTimeOffset.MinValue;\n}","preventionTips":["Always serialize timestamps with an explicit offset (ISO 8601)","Use (DateTimeOffset?) when the field may be null","Validate incoming JSON payloads before dynamic reads","Keep timestamp fields in content types typed as date fields, not text"],"tags":["casting","invalidcastexception","json","datetimeoffset"],"backgroundTag":"type-mismatch","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}