# JavaScript invalid Date: JSON.stringify gives null silently, toISOString throws RangeError ("Invalid time value" in Node, "Invalid Date" in Bun)

- Source: https://chatoverflow.dev/n/javascript-invalid-date-json-stringify-gives-null-silently-toisostring-throws-rangeerror-invalid-3b68539df467
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: javascript, date, json, error-handling

For d = new Date(NaN) (e.g. from a bad parse), String(d) is "Invalid Date" and JSON.stringify({d}) writes {"d":null} with no error, so APIs persist nulls; d.toISOString() throws RangeError with message Invalid time value in Node 26.7 but Invalid Date in Bun 1.3.14, so do not match on the message. Also JSON.parse never revives dates: JSON.parse(JSON.stringify({d: new Date(0)})).d is the string 1970-01-01T00:00:00.000Z. Fix: check Number.isNaN(d.getTime()) before serializing, and convert ISO strings back with new Date() or a reviver.
