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:24

Quick 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.

  1. Log the raw value that was passed to new Date() for the failing record.
  2. Guard formatting: if Number.isNaN(d.getTime()), handle the missing or bad date instead of calling toISOString().
  3. Parse known non-ISO formats explicitly (a date library parse with a format string) instead of relying on new Date(string).
  4. Check for undefined fields: a renamed column or API field turns every date into new Date(undefined).
  5. Validate dates at the edge (request schema, CSV import) so invalid ones are rejected with a clear message.
typescript
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.

Plans from $29/moSee what your agent readsSee the setupCancel any time from the billing portal

Related errors

More in JSON, parsing and validation

References: MDN: Date.prototype.toISOString (throws RangeError on an invalid date), MDN: RangeError: invalid date