{"record":{"id":"010f19f0971939a6","repo":"JamesNK/Newtonsoft.Json","slug":"unexpected-value-when-converting-date-expected-da","errorCode":null,"errorMessage":"Unexpected value when converting date. Expected DateTime or DateTimeOffset, got {0}.","messagePattern":"Unexpected value when converting date\\. Expected DateTime or DateTimeOffset, got (.+?)\\.","errorType":"exception","errorClass":"JsonSerializationException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Converters/IsoDateTimeConverter.cs","lineNumber":107,"sourceCode":"                }\n\n                text = dateTime.ToString(_dateTimeFormat ?? DefaultDateTimeFormat, Culture);\n            }\n#if HAVE_DATE_TIME_OFFSET\n            else if (value is DateTimeOffset dateTimeOffset)\n            {\n                if ((_dateTimeStyles & DateTimeStyles.AdjustToUniversal) == DateTimeStyles.AdjustToUniversal\n                    || (_dateTimeStyles & DateTimeStyles.AssumeUniversal) == DateTimeStyles.AssumeUniversal)\n                {\n                    dateTimeOffset = dateTimeOffset.ToUniversalTime();\n                }\n\n                text = dateTimeOffset.ToString(_dateTimeFormat ?? DefaultDateTimeFormat, Culture);\n            }\n#endif\n            else\n            {\n                throw new JsonSerializationException(\"Unexpected value when converting date. Expected DateTime or DateTimeOffset, got {0}.\".FormatWith(CultureInfo.InvariantCulture, ReflectionUtils.GetObjectType(value)!));\n            }\n\n            writer.WriteValue(text);\n        }\n\n        /// <summary>\n        /// Reads the JSON representation of the object.\n        /// </summary>\n        /// <param name=\"reader\">The <see cref=\"JsonReader\"/> to read from.</param>\n        /// <param name=\"objectType\">Type of the object.</param>\n        /// <param name=\"existingValue\">The existing value of object being read.</param>\n        /// <param name=\"serializer\">The calling serializer.</param>\n        /// <returns>The object value.</returns>\n        public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)\n        {\n            bool nullable = ReflectionUtils.IsNullableType(objectType);\n            if (reader.TokenType == JsonToken.Null)\n            {","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Converters/IsoDateTimeConverter.cs#L89-L125","documentation":"Thrown by IsoDateTimeConverter.WriteJson when the value being serialized is neither a DateTime nor a DateTimeOffset. The converter formats dates to ISO strings; any other runtime type (and the DateTimeOffset branch being compiled out on platforms without HAVE_DATE_TIME_OFFSET) falls through to this JsonSerializationException.","triggerScenarios":"Applying IsoDateTimeConverter to a property/type whose runtime value is not DateTime or DateTimeOffset (e.g. a DateOnly, TimeSpan, string, or custom date struct). Also on platforms where DateTimeOffset support is compiled out, passing a DateTimeOffset triggers it.","commonSituations":"Using IsoDateTimeConverter with .NET 6+ DateOnly/TimeOnly which are not handled. A refactor changed a property from DateTime to string but left the converter attribute. A polymorphic object whose runtime type is unexpected at serialization time.","solutions":["Ensure the value is a DateTime or DateTimeOffset before it reaches IsoDateTimeConverter.","For DateOnly/TimeOnly, convert to DateTime before serializing or use a dedicated converter.","Remove the [JsonConverter(typeof(IsoDateTimeConverter))] attribute from properties that are no longer dates.","If serializing a custom date type, map it to DateTime first or implement a purpose-built converter."],"exampleFix":"// before: DateOnly is not DateTime/DateTimeOffset\n[JsonConverter(typeof(IsoDateTimeConverter))]\npublic DateOnly BirthDate { get; set; }\n\n// after: use a DateOnly-aware approach\n[JsonConverter(typeof(DateOnlyJsonConverter))]\npublic DateOnly BirthDate { get; set; }","handlingStrategy":"type-guard","validationCode":"object value = GetValue();\nif (value != null && !(value is DateTime) && !(value is DateTimeOffset))\n    throw new InvalidOperationException($\"IsoDateTimeConverter cannot serialize {value.GetType()}\");","typeGuard":"static bool IsDateType(object v) =>\n    v is DateTime || v is DateTimeOffset;","tryCatchPattern":"try { conv.WriteJson(writer, value, serializer); }\ncatch (JsonSerializationException ex) when (ex.Message.Contains(\"Expected DateTime or DateTimeOffset\"))\n{\n    throw new InvalidOperationException($\"IsoDateTimeConverter got {value?.GetType()}\", ex);\n}","preventionTips":["Only apply IsoDateTimeConverter to DateTime/DateTimeOffset members.","For DateOnly/TimeOnly, use dedicated converters or convert to DateTime first.","Re-audit converter attributes after changing a property's type."],"tags":["serialization","converter","datetime","type-mismatch"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}