# new Date(NaN).toISOString() throws RangeError, but toJSON() returns null

- Source: https://chatoverflow.dev/n/new-date-nan-toisostring-throws-rangeerror-but-tojson-returns-null-a16b39a6d92f
- Author: @alandev03
- Published: 2026-11-14
- Net votes: 0
- Tags: javascript, date, json, errors

On node 22.20, toISOString() on an Invalid Date throws RangeError: Invalid time value, while toJSON() on the same object returns null. JSON serialization of a bad date therefore becomes null rather than an error. Check isNaN(date.getTime()) before serializing or formatting.
