RangeError: Invalid time value
Node.js · updated 2026-11-04
Thrown when an Invalid Date is formatted: Date.prototype.toISOString(), Intl.DateTimeFormat#format, and date libraries such as date-fns format(). The bad Date was created earlier, often far from the line in the stack, from a string or value the Date parser could not read.
RangeError: Invalid time value
at Date.toISOString (<anonymous>)
at serializeOrder (/app/dist/orders/serialize.js:14:38)
at /app/dist/routes/orders.js:31:24Quick fix
Find where the Date was created and log the input. new Date(undefined), new Date("") and strings outside the ISO 8601 format (or with impossible values such as month 13) give Invalid Date. Check with Number.isNaN(date.getTime()) where the value comes in, parse non-ISO formats explicitly, and decide what to do with missing dates before formatting.
- Log the raw value that was passed to new Date() for the failing record.
- Guard formatting: if Number.isNaN(d.getTime()), handle the missing or bad date instead of calling toISOString().
- Parse known non-ISO formats explicitly (a date library parse with a format string) instead of relying on new Date(string).
- Check for undefined fields: a renamed column or API field turns every date into new Date(undefined).
- Validate dates at the edge (request schema, CSV import) so invalid ones are rejected with a clear message.
function toDate(value: unknown): Date | null {
if (value === null || value === undefined || value === '') return null;
const d = value instanceof Date ? value : new Date(value as string | number);
return Number.isNaN(d.getTime()) ? null : d;
}
const shippedAt = toDate(row.shipped_at);
const payload = { shippedAt: shippedAt ? shippedAt.toISOString() : null };Why it happens
A missing field
new Date(undefined) is Invalid Date. A renamed column, an optional API field or a typo in the property name passes undefined. (new Date(null) is 1970-01-01, which is valid, so nulls fail differently: a 1970 date in the output.)
A string the parser cannot read
Only ISO 8601 strings parse the same in every JavaScript engine. Formats like "31/12/2026" or "2026-12-31 25:00" give Invalid Date in Node, and other formats parse differently between engines.
Impossible values
Out-of-range parts in an ISO string, such as month 13 or hour 25, make the whole date invalid.
Formatted far from where it was created
The Date is made when a record is read but only formatted later, in a serializer, a log line or a template, so the stack points at the formatter. JSON.stringify does not throw: it writes null for an Invalid Date, which hides the problem until something calls toISOString().
Other fixes
- Store and send dates as ISO 8601 strings in UTC; they parse the same everywhere.
- In schema validation (Zod, Joi), use a date type with coercion so bad dates fail at the edge with a 400.
Same error, other wording
- RangeError: invalid date
- RangeError: Invalid time value (at format, date-fns)
- RangeError: Invalid time value (at Intl.DateTimeFormat.format)
Let your coding agent catch the next one
Runheed records this error in production with the stack trace, the breadcrumbs before it and the agent session that shipped the code, and serves it to Claude Code, Cursor or any MCP client. With it connected, you can ask:
> Find the "RangeError: Invalid time value" errors in production, trace the Date back to the input value that created it, and validate or parse that input so invalid dates are handled before formatting.
Related errors
- TypeError: Cannot read properties of undefined (reading 'x')
- ZodError: [ { "code": "invalid_type", "expected": "string", "received": "undefined"...
- SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
More in JSON, parsing and validation
- SyntaxError: Unexpected end of JSON inputNode.js
- TypeError: Converting circular structure to JSONNode.js
- json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)Python
- TypeError: Object of type datetime is not JSON serializablePython
- pydantic_core._pydantic_core.ValidationError: 1 validation error for User email Field...Python
- UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start bytePython
References: MDN: Date.prototype.toISOString (throws RangeError on an invalid date), MDN: RangeError: invalid date