{"record":{"id":"d8bd6180c4b4cff3","repo":"karatelabs/karate","slug":"invalid-time-value","errorCode":null,"errorMessage":"Invalid time value","messagePattern":"Invalid time value","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsDatePrototype.java","lineNumber":191,"sourceCode":"        String h;\n        if (\"string\".equals(hint) || \"default\".equals(hint)) {\n            h = \"string\";\n        } else if (\"number\".equals(hint)) {\n            h = \"number\";\n        } else {\n            throw JsErrorException.typeError(\"invalid hint to Symbol.toPrimitive: \" + hint);\n        }\n        Object thisObj = context.getThisObject();\n        if (!(thisObj instanceof ObjectLike ol)) {\n            throw JsErrorException.typeError(\"Date.prototype[@@toPrimitive] called on non-object\");\n        }\n        return Terms.ordinaryToPrimitive(ol, h, (CoreContext) context);\n    }\n\n    private Object toISOString(Context context, Object[] args) {\n        JsDate d = requireDate(context);\n        if (d.isInvalid()) {\n            throw JsErrorException.rangeError(\"Invalid time value\");\n        }\n        return formatIso(d);\n    }\n\n    private Object toUTCString(Context context, Object[] args) {\n        JsDate d = requireDate(context);\n        if (d.isInvalid()) {\n            return \"Invalid Date\";\n        }\n        return formatUtc(d);\n    }\n\n    /**\n     * Spec Date.prototype.toJSON: works on ANY object (not just Date) per\n     * §21.4.4.37. ToPrimitive(this, hint Number); if Number+non-finite return null;\n     * else Invoke(O, \"toISOString\"). The unusual generic-this pattern is why\n     * this method does NOT use {@link #requireDate}.\n     */","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsDatePrototype.java#L173-L209","documentation":"toISOString() must produce a valid ISO-8601 string, which is impossible for an invalid date (the internal time value is NaN, e.g. from new Date(NaN) or new Date('garbage')). Karate throws this RangeError per spec §20.4.4.36 step 3 instead of emitting 'Invalid Date' like toString() does.","triggerScenarios":"new Date(NaN).toISOString(), new Date('not-a-date').toISOString(), new Date(Infinity).toISOString(), or any Date whose valueOf() is NaN flowing into JSON.stringify (which calls toJSON -> toISOString).","commonSituations":"Parsing untrusted/invalid date strings from API responses or user input, arithmetic like date + undefined yielding NaN, JSON.stringify of an object holding an invalid Date.","solutions":["Check isNaN(date.getTime()) before calling toISOString() and handle the invalid case.","Sanitize/validate the date input string or number before constructing the Date.","Fall back to String(date) ('Invalid Date') or a sentinel like null when serialization is needed."],"exampleFix":"// before\nconst iso = new Date(input).toISOString(); // RangeError on bad input\n// after\nconst d = new Date(input);\nconst iso = isNaN(d.getTime()) ? null : d.toISOString();","handlingStrategy":"validation","validationCode":"const d = new Date(input); if (isNaN(d.getTime())) throw new Error('invalid date: ' + input);","typeGuard":"function isValidDate(v) { return v instanceof Date && !isNaN(v.getTime()); }","tryCatchPattern":"try { return d.toISOString(); } catch (e) { if (e instanceof RangeError && /Invalid time value/.test(e.message)) return null; throw e; }","preventionTips":["Always check isNaN(date.getTime()) before ISO serialization.","Validate date strings/numbers at the boundary (API input, config).","Use a custom JSON replacer that guards invalid Dates.","Avoid arithmetic that can silently produce NaN (date + undefined)."],"tags":["javascript","date","range-error","invalid-date"],"backgroundTag":"invalid-date-format","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}