{"record":{"id":"eb84f14383765711","repo":"JamesNK/Newtonsoft.Json","slug":"can-not-convert-0-to-timespan","errorCode":null,"errorMessage":"Can not convert {0} to TimeSpan.","messagePattern":"Can not convert (.+?) to TimeSpan\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JToken.cs","lineNumber":1479,"sourceCode":"            if (v.Value is byte[] bytes)\n            {\n                return new Guid(bytes);\n            }\n\n            return (v.Value is Guid guid) ? guid : new Guid(Convert.ToString(v.Value, CultureInfo.InvariantCulture)!);\n        }\n\n        /// <summary>\n        /// Performs an explicit conversion from <see cref=\"JToken\"/> to <see cref=\"TimeSpan\"/>.\n        /// </summary>\n        /// <param name=\"value\">The value.</param>\n        /// <returns>The result of the conversion.</returns>\n        public static explicit operator TimeSpan(JToken value)\n        {\n            JValue? v = EnsureValue(value);\n            if (v == null || !ValidateToken(v, TimeSpanTypes, false))\n            {\n                throw new ArgumentException(\"Can not convert {0} to TimeSpan.\".FormatWith(CultureInfo.InvariantCulture, GetType(value)));\n            }\n\n            return (v.Value is TimeSpan span) ? span : ConvertUtils.ParseTimeSpan(Convert.ToString(v.Value, CultureInfo.InvariantCulture)!);\n        }\n\n        /// <summary>\n        /// Performs an explicit conversion from <see cref=\"JToken\"/> to <see cref=\"Nullable{T}\"/> of <see cref=\"TimeSpan\"/>.\n        /// </summary>\n        /// <param name=\"value\">The value.</param>\n        /// <returns>The result of the conversion.</returns>\n        public static explicit operator TimeSpan?(JToken? value)\n        {\n            if (value == null)\n            {\n                return null;\n            }\n\n            JValue? v = EnsureValue(value);","sourceCodeStart":1461,"sourceCodeEnd":1497,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JToken.cs#L1461-L1497","documentation":"Thrown by the explicit cast operator 'operator TimeSpan(JToken)' (NON-nullable) when the token is not a TimeSpan-compatible JValue. ArgumentException {0} is the resolved JTokenType; allowed types are TimeSpanTypes ({String, Comment, Raw, TimeSpan}) — the narrowest set. Non-nullable overload, so a JSON null token ALSO throws. Only a TimeSpan or a culture-invariant TimeSpan string is accepted.","triggerScenarios":"Calling (TimeSpan)token[\"duration\"] when the JValue Type is Integer, Float, Boolean, Date, Guid, Uri, Bytes, Null, Object, or Array, or the node is a JObject/JArray (EnsureValue null). An epoch-integer duration or a number is rejected because TimeSpan parsing from numbers is ambiguous.","commonSituations":"Duration field emitted as an integer (ticks/millis/seconds) instead of a TimeSpan string; null duration in some records cast to non-nullable TimeSpan; field changed to an ISO duration object.","solutions":["Use the Nullable overload (TimeSpan?)token[\"duration\"] so JSON null returns null.","If the value is numeric (ticks/millis), convert it yourself (TimeSpan.FromMilliseconds(...)) instead of casting.","Guard token[\"duration\"]?.Type is String or TimeSpan before the non-nullable cast, and use TimeSpan.TryParse.","Normalize the producer to emit standard TimeSpan strings like \"00:01:30\"."],"exampleFix":"// before\nTimeSpan duration = (TimeSpan)payload[\"duration\"]; // throws on integer millis\n\n// after\nTimeSpan duration = TimeSpan.TryParse((string?)payload[\"duration\"], out var ts) ? ts : TimeSpan.Zero;","handlingStrategy":"validation","validationCode":"TimeSpan duration = TimeSpan.TryParse((string?)payload[\"duration\"], out var ts) ? ts : TimeSpan.Zero;","typeGuard":"static bool IsTimeSpanLike(JToken? t) =>\n    t is JValue v && (v.Type == JTokenType.TimeSpan || v.Type == JTokenType.String);","tryCatchPattern":"TimeSpan duration;\ntry { duration = (TimeSpan)payload[\"duration\"]; }\ncatch (ArgumentException) { duration = TimeSpan.Zero; }","preventionTips":["Use the Nullable overload (TimeSpan?) for fields that may be JSON null.","Convert epoch integers via TimeSpan.FromMilliseconds/FromSeconds instead of casting.","TimeSpanTypes is the narrowest set — only String/TimeSpan are accepted."],"tags":["json","linq-to-json","type-cast","jtoken","timespan","newtonsoft-json"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}