karatelabs/karate · error · JsErrorException

this is not a Date object

Error message

this is not a Date object

What it means

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.

Solutions

  1. Call the method on a real Date instance: new Date(x).getTime()
  2. If using .call/.apply, pass an actual Date as this
  3. Validate before use: if (!(v instanceof Date)) throw new TypeError('expected Date')
  4. Convert numeric timestamps explicitly rather than relying on Date methods: typeof v === 'number' ? v : new Date(v).getTime()

Example fix

// before
const getT = Date.prototype.getTime;
getT.call(1718000000000); // TypeError: this is not a Date object
// after
const getT = (v) => new Date(v).getTime();
Defensive patterns

Strategy: type-guard

Validate before calling

function requireDate(v) {
  if (!(v instanceof Date)) throw new TypeError('expected a Date instance');
  if (isNaN(v.getTime())) throw new TypeError('invalid Date');
  return v;
}

Type guard

const isDate = (v) => v instanceof Date && !isNaN(v.getTime());

Try / catch

let t;
try { t = maybeD.getTime(); } catch (e) {
  if (e instanceof TypeError && /not a Date/.test(e.message)) t = new Date(maybeD).getTime();
  else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/17c30681ec0fc9ed. Report an issue: GitHub.

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/JsDatePrototype.java:133

        install("getUTCMonth", 0, this::getUTCMonth);
        install("getUTCDate", 0, this::getUTCDate);
        install("getUTCDay", 0, this::getUTCDay);
        install("getUTCHours", 0, this::getUTCHours);
        install("getUTCMinutes", 0, this::getUTCMinutes);
        install("getUTCSeconds", 0, this::getUTCSeconds);
        install("getUTCMilliseconds", 0, this::getUTCMilliseconds);
    }

    /**
     * Spec thisTimeValue: returns the JsDate's [[DateValue]] or TypeErrors if
     * {@code this} is not a Date.
     */
    private static JsDate requireDate(Context context) {
        Object thisObj = context.getThisObject();
        if (thisObj instanceof JsDate d) {
            return d;
        }
        throw JsErrorException.typeError("this is not a Date object");
    }

    /** Number-valued time helper, returns NaN-Double for invalid dates, Long otherwise. */
    private static Object boxTime(double v) {
        if (Double.isNaN(v)) {
            return Double.NaN;
        }
        return (long) v;
    }

    private static ZonedDateTime localZdt(JsDate d) {
        return ZonedDateTime.ofInstant(Instant.ofEpochMilli(d.getTime()), ZoneId.systemDefault());
    }

    private static ZonedDateTime utcZdt(JsDate d) {
        return ZonedDateTime.ofInstant(Instant.ofEpochMilli(d.getTime()), ZoneOffset.UTC);
    }

View on GitHub (pinned to a22eb90246)