Why toISOString() throws RangeError: Invalid time value
RangeError: Invalid time valueShort answer
In Node.js this error means toISOString() was called on a Date that is invalid. The Date was built from a string or number JavaScript could not turn into a time. Check the Date with getTime() before converting it, and pass an ISO 8601 string or a sane millisecond timestamp when you build it.
Fix it fast
1Find out which cause you have
Print the Date you are about to convert. An invalid Date shows as Invalid Date and its getTime() is NaN.
const d = new Date(input); console.log(String(d), d.getTime(), typeof input, input);2Match what you see, then jump to its fix
- 1
String(d) prints Invalid Date and getTime() is NaNThe input string is not something Date can parseFix - 2
The input is a number, and getTime() is NaNThe value is a timestamp outside the range Date can holdFix - 3
JSON.stringify gives null for a date field, and getDate() gives NaN, with no errorOnly toISOString() throws: other code paths hide the bad DateFix
- 1
What does the error look like?
RangeError: Invalid time valueat Date.toISOString (<anonymous>)
Cause 1 of 3
The input string is not something Date can parse
1Check
Log the raw value you pass to new Date(). Empty strings, undefined, and text like a localised date are typical.
console.log(JSON.stringify(input));
console.log(Number.isNaN(new Date(input).getTime()));2Fix
Send and store dates as ISO 8601 strings, and guard the conversion so a bad value becomes null instead of a crash.
function safeToISO(input) {
const d = new Date(input);
return Number.isNaN(d.getTime()) ? null : d.toISOString();
}3Verify
Call it with a good and a bad value.
console.log(safeToISO("2011-10-05T14:48:00Z")); // 2011-10-05T14:48:00.000Z
console.log(safeToISO("nothing")); // nullConfirmed by developer.mozilla.org
Cause 2 of 3
The value is a timestamp outside the range Date can hold
1Check
A date becomes invalid when its timestamp is out of bounds. Look for a number that was multiplied twice, or an id or counter passed by mistake.
console.log(typeof input, input);
console.log(new Date(input).getTime());2Fix
Find where the number is produced and pass the real millisecond timestamp, then keep the same NaN guard in front of toISOString().
const d = new Date(Number(input));
if (Number.isNaN(d.getTime())) throw new Error("bad timestamp: " + input);3Verify
Convert a known good timestamp.
node -e "console.log(new Date(Date.now()).toISOString())"Confirmed by developer.mozilla.org
Cause 3 of 3
Only toISOString() throws: other code paths hide the bad Date
1Check
Most Date methods return NaN or null on an invalid Date. Only toISOString() throws, so the error appears far from where the Date went bad.
const bad = new Date("nothing");
console.log(bad.getDate(), bad.toJSON(), JSON.stringify({ date: bad }));2Fix
Validate at the edge, where the value enters your code (request body, database row), not where it is printed.
const d = new Date(req.body.startsAt);
if (Number.isNaN(d.getTime())) return res.status(400).json({ error: "startsAt is not a valid date" });3Verify
Send a bad value and expect a 400 instead of a crash.
curl -s -X POST localhost:3000/events -H 'content-type: application/json' -d '{"startsAt":"nothing"}'Confirmed by developer.mozilla.org
How do I stop it coming back?
- Parse and validate every date where it enters the app, and keep only ISO 8601 strings after that.
- Never call toISOString() on a value you did not just validate.
- Log the raw input next to the error so the next report names the bad value.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix