JavaScript Date.parse(): Explicit ISO and Invalid Dates
Quick answer
Date.parse returns a numeric time value or NaN. Use the specified date-time string format and an explicit offset for portable instant inputs.
How it works
Date.parse("2024-05-01T12:00:00Z") uses UTC; an explicit +02:00 denotes a time two hours ahead of UTC. ISO date-only strings without an offset are interpreted as UTC, but date-time strings without an offset are interpreted as local time. Other parsing formats can be implementation-dependent. Check Number.isNaN(d.getTime()); parsing alone does not ensure a strict business-date contract, because some calendar inputs may normalize.
console.log(Date.parse("1970-01-01T01:00:00+01:00"));Expected output in Node.js:
0The offset identifies the same instant as the epoch.
const d=new Date("not-a-date"); console.log(Boolean(d),Number.isNaN(d.getTime()));Expected output in Node.js:
true trueObject truthiness does not depend on the internal time value.
Common mistakes
An invalid Date is still an object and is truthy. Do not use if (date) as validity check.
Practice
Parse a fixed ISO date-time or a deliberately invalid token; return the timestamp or null when invalid.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Date.parse(): Explicit ISO and Invalid Dates — Knowledge Check
Apply the taught concept and predict the output.
How is an ISO date-only string with no offset interpreted by Date.parse?
Which check detects an invalid Date time value?
What should a portable instant string include?
What is the exact console output in JavaScript Date.parse(): Explicit ISO and Invalid Dates example 1?
console.log(Date.parse("1970-01-01T01:00:00+01:00"));What is the exact console output in JavaScript Date.parse(): Explicit ISO and Invalid Dates example 2?
const d=new Date("not-a-date"); console.log(Boolean(d),Number.isNaN(d.getTime()));