toISOString fails because Date.now() returns a number, not a Date
toISOString is not a function (TypeError)Short answer
Date.now() returns a number of milliseconds since 1 January 1970 UTC, and numbers have no toISOString method. That method belongs to Date objects. Build one with new Date() or new Date(timestamp), then call toISOString on it. An invalid date throws a RangeError instead.
Fix it fast
1Find out which cause you have
Print the type of the value you call toISOString on. Anything other than a Date object (a number or a string) is the cause.
node -e "console.log(typeof Date.now(), typeof new Date())"2Match what you see, then jump to its fix
- 1
toISOString is not a function (TypeError)Date.now() gives a numberFix - 2
The same TypeError, on a value read from JSON, a database row or a query stringA stored timestamp comes back as a string or numberFix - 3
RangeError thrown by toISOString on an invalid dateThe value cannot be turned into a valid dateFix
- 1
What does the error look like?
toISOString is not a function (TypeError)RangeError thrown by toISOString on an invalid date
Cause 1 of 3
Date.now() gives a number
1Check
Date.now() is a static call that returns the current time as a number of milliseconds. A Date instance comes only from new Date().
node -e "console.log(Date.now(), new Date().toISOString())"2Fix
Use new Date() for the current time. If you already hold a millisecond timestamp, pass it to the constructor.
const now = new Date().toISOString();
const fromStamp = new Date(Date.now()).toISOString();3Verify
The output matches the YYYY-MM-DDTHH:mm:ss.sssZ shape.
node -e "console.log(new Date().toISOString())"Confirmed by developer.mozilla.org
Cause 2 of 3
A stored timestamp comes back as a string or number
1Check
toISOString exists only on Date instances. A value parsed from JSON stays a string or number, so it has to be turned back into a Date first.
node -e "const v = JSON.parse('{\"at\":1700000000000}').at; console.log(typeof v)"2Fix
Wrap the value in new Date() before calling the method.
const iso = new Date(row.createdAt).toISOString();3Verify
Run the line with a real row and print the result.
node -e "console.log(new Date(1700000000000).toISOString())"Confirmed by developer.mozilla.org
Cause 3 of 3
The value cannot be turned into a valid date
1Check
toISOString throws a RangeError when the date is invalid or its year cannot be written in the format. An undefined field or an unparsable string makes new Date() produce an invalid date.
node -e "console.log(new Date(undefined).getTime())"2Fix
Check the date before formatting it, and handle the bad input where it enters the system.
const d = new Date(input);
if (Number.isNaN(d.getTime())) {
throw new Error('bad date: ' + input);
}
const iso = d.toISOString();3Verify
Pass a good and a bad value and see that only the bad one stops.
node -e "console.log(Number.isNaN(new Date('nope').getTime()))"Confirmed by developer.mozilla.org
How do I stop it coming back?
- Keep dates as Date objects in code and convert to strings only at the edge (JSON out, database in).
- Validate incoming date fields before they reach formatting code.
- Add a type annotation or schema so a number cannot be passed where a Date is expected.
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