{"record":{"id":"17c30681ec0fc9ed","repo":"karatelabs/karate","slug":"this-is-not-a-date-object","errorCode":null,"errorMessage":"this is not a Date object","messagePattern":"this is not a Date object","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsDatePrototype.java","lineNumber":133,"sourceCode":"        install(\"getUTCMonth\", 0, this::getUTCMonth);\n        install(\"getUTCDate\", 0, this::getUTCDate);\n        install(\"getUTCDay\", 0, this::getUTCDay);\n        install(\"getUTCHours\", 0, this::getUTCHours);\n        install(\"getUTCMinutes\", 0, this::getUTCMinutes);\n        install(\"getUTCSeconds\", 0, this::getUTCSeconds);\n        install(\"getUTCMilliseconds\", 0, this::getUTCMilliseconds);\n    }\n\n    /**\n     * Spec thisTimeValue: returns the JsDate's [[DateValue]] or TypeErrors if\n     * {@code this} is not a Date.\n     */\n    private static JsDate requireDate(Context context) {\n        Object thisObj = context.getThisObject();\n        if (thisObj instanceof JsDate d) {\n            return d;\n        }\n        throw JsErrorException.typeError(\"this is not a Date object\");\n    }\n\n    /** Number-valued time helper, returns NaN-Double for invalid dates, Long otherwise. */\n    private static Object boxTime(double v) {\n        if (Double.isNaN(v)) {\n            return Double.NaN;\n        }\n        return (long) v;\n    }\n\n    private static ZonedDateTime localZdt(JsDate d) {\n        return ZonedDateTime.ofInstant(Instant.ofEpochMilli(d.getTime()), ZoneId.systemDefault());\n    }\n\n    private static ZonedDateTime utcZdt(JsDate d) {\n        return ZonedDateTime.ofInstant(Instant.ofEpochMilli(d.getTime()), ZoneOffset.UTC);\n    }\n","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsDatePrototype.java#L115-L151","documentation":"Date.prototype methods in Karate's JS engine require `this` to be an internal JsDate instance; calling them with another receiver throws this TypeError. The engine does not auto-convert primitives or foreign objects, so any detached or borrowed Date method fails.","triggerScenarios":"Date.prototype.getTime.call({}), unbound method extraction (const t = d.getTime; t()), calling date methods inside callbacks with the wrong this, or invoking a Date method on a plain object masquerading as a date.","commonSituations":"Function-style utilities that call date methods with .call/.apply; code ported between JS engines with different strictness; mock objects in tests standing in for real Dates; Karate JS snippets storing timestamps as numbers then calling date methods on them.","solutions":["Call the method on a real Date instance: new Date(x).getTime()","If using .call/.apply, pass an actual Date as this","Validate before use: if (!(v instanceof Date)) throw new TypeError('expected Date')","Convert numeric timestamps explicitly rather than relying on Date methods: typeof v === 'number' ? v : new Date(v).getTime()"],"exampleFix":"// before\nconst getT = Date.prototype.getTime;\ngetT.call(1718000000000); // TypeError: this is not a Date object\n// after\nconst getT = (v) => new Date(v).getTime();","handlingStrategy":"type-guard","validationCode":"function requireDate(v) {\n  if (!(v instanceof Date)) throw new TypeError('expected a Date instance');\n  if (isNaN(v.getTime())) throw new TypeError('invalid Date');\n  return v;\n}","typeGuard":"const isDate = (v) => v instanceof Date && !isNaN(v.getTime());","tryCatchPattern":"let t;\ntry { t = maybeD.getTime(); } catch (e) {\n  if (e instanceof TypeError && /not a Date/.test(e.message)) t = new Date(maybeD).getTime();\n  else throw e;\n}","preventionTips":["Validate instanceof Date before calling date methods via .call/.apply","Construct new Date(x) for numeric timestamps instead of borrowing methods","Beware mock objects in tests that mimic dates without being Dates","Keep date values as Dates end-to-end; convert at boundaries only"],"tags":["javascript","date","type-error","this-binding"],"backgroundTag":"type-mismatch","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}