Short answer
Log the exact value passed to new Date(). Pass ISO 8601 strings (2026-12-31 or 2026-12-31T10:00:00Z), not local formats such as 31-12-2026 or 31/12/2026, which are invalid or parsed by browser-specific rules. Guard against undefined and empty values before creating the date. Check validity with Number.isNaN(date.getTime()) before calling toISOString() or formatting it. For other formats, parse the parts yourself or use a date library’s explicit format parser.
Why the date is invalid, and the fix
01
A non-standard date string
“This section contains implementation-specific behavior that may be inconsistent across browsers or different versions of browsers.” MDN, Date.parse(): non-standard date strings
Only the ISO 8601 format is guaranteed to parse the same everywhere. Strings like 31-12-2026 give Invalid Date, 31/12/2026 is read with 31 as the month, and other formats such as 12/31/2026 follow implementation-specific rules that can differ by browser and version. Send and store ISO strings, and build dates from parts when you have to.
new Date('2026-12-31T10:00:00Z'); // reliable // from '31/12/2026' const [d, m, y] = '31/12/2026'.split('/').map(Number); const date = new Date(y, m - 1, d); // months start at 002
An empty, undefined or wrong value
new Date(undefined) and new Date('') are invalid, while new Date(null) quietly becomes January 1, 1970. A missing API field or form value is the usual source. Check the input first.
function toDate(value) { if (value == null || value === '') return null; const date = new Date(value); return Number.isNaN(date.getTime()) ? null : date; }03
toISOString() on an invalid date
“An invalid date is produced when you attempt to parse an invalid date string, or set the timestamp to an out-of-bounds value.” MDN, RangeError: invalid date
Most date methods return NaN for an invalid date, but toISOString() throws RangeError: Invalid time value. JSON.stringify does not throw: it writes null for an invalid date, which can quietly corrupt saved data. Validate before you serialize.
04
Seconds instead of milliseconds
new Date() takes milliseconds. A Unix timestamp in seconds (from many APIs and databases) gives a date in January 1970 instead of an error. Multiply by 1000.
const date = new Date(createdAtSeconds * 1000);
Checked against the vendor’s docs on : MDN: RangeError: invalid date; MDN: Date.parse().
Rather hand it over?
Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.
- from $450App Fix TicketOne reproducible bug in an existing mobile or web app, diagnosed and fixed.See scope and start
- from $450AI-Built App RescueA Lovable, Bolt or Cursor app checked end to end in 3 days, then its worst problems fixed at a fixed price.No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.See scope and start
Related: “Cannot read properties of undefined” · “Unexpected end of JSON input” · TypeError: “x is not a constructor”