Invalid Date Construction
Quick summary
You passed a string the Date parser cannot understand — the result is an Invalid Date, and getTime() returns NaN.
Why this happens
Date string in a non-standard format (e.g., '2023/13/45' or '2023-15-99')
Mixing month names with numeric formats the parser does not recognize
Passing a value of the wrong type (e.g., an object) to the Date constructor
Minimal example
const d = new Date('2023-13-45');
console.log(d.getTime()); // NaN — Invalid Dateconst d = new Date('2023-12-15');
console.log(d.getTime()); // valid timestampMonth 13 and day 45 do not exist, so the parser yields an Invalid Date. Using a real calendar date produces a valid timestamp.
How to diagnose
Call isNaN(date.getTime()) — true means the Date is invalid
Print the original input string and compare it with a known-good format
Check the date components (month 0-11, day within month range) for out-of-range values
How to fix
Use ISO 8601 format (YYYY-MM-DD) which the parser reliably accepts
Validate the input with a date library (date-fns, dayjs) before constructing
Check isNaN(date.getTime()) and fall back to a default date when invalid
How to prevent
Always use ISO 8601 strings or numeric constructor arguments
Validate user-supplied date strings with a library before passing to Date
Related Resources
Related Glossary
Related Lessons
- Numbers and Math
Learn about dates and numbers.