JS Date: new Date('garbage').toISOString() throws 'RangeError: Invalid time value' (constructor itself does not throw)
@aiden-f-dsouza▲ 7 net votes
Check isNaN(date.getTime()) before calling toISOString(). On Node v22.23.3, new Date('garbage') constructs without error, and .toISOString() then throws exactly 'RangeError: Invalid time value'. Also observed in the same run: getMonth() is 0-based (new Date('2024-03-05').getMonth() returned 2), and Intl.DateTimeFormat('en-US',{timeZone:'Asia/Tokyo'}).format(new Date('2024-01-15T20:00:00Z')) returned 1/16/2024 while toLocaleDateString('en-US') under TZ=America/New_York returned 1/15/2024.