Ad Space — Top Banner

Invalid Date

JavaScript Programming Language

Severity: Minor

What Does This Error Mean?

Invalid Date means JavaScript's Date constructor received a string it could not parse. Instead of throwing an error, JavaScript creates a Date object that represents 'nothing'. Calling .toString() on it returns the literal text 'Invalid Date'.

Affected Models

  • Chrome DevTools
  • Firefox DevTools
  • Node.js
  • React
  • Vue.js

Common Causes

  • Passing a date string in a format JavaScript does not recognize (e.g., DD/MM/YYYY)
  • Passing undefined or null to the Date constructor
  • Using a date format that works in Chrome but fails in Safari (browser inconsistency)
  • The date string came from an API and has an unexpected format
  • Passing a timestamp as a string instead of a number (e.g., '1712000000' instead of 1712000000)

How to Fix It

  1. Check what value you are passing to new Date() by logging it first.

    console.log(myDateValue) before new Date(myDateValue). If it logs undefined or null, fix the source of the value.

  2. Convert your date string to ISO 8601 format before parsing.

    ISO format looks like: 2026-04-01 or 2026-04-01T12:00:00Z. This is the only format reliably parsed in all browsers.

  3. Replace slashes with dashes if you are getting date strings like 2026/04/01.

    const fixed = myString.replace(/[/]/g, '-'). Slash-separated dates fail in Safari even though they work in Chrome.

  4. Use a date library like date-fns or Day.js for reliable cross-browser parsing.

    Example with date-fns: parse('01/04/2026', 'dd/MM/yyyy', new Date()). These libraries handle all format variations.

  5. Validate the date after creation before using it.

    Check if a date is valid with: isNaN(myDate.getTime()). If getTime() returns NaN, the date is invalid.

When to Call a Professional

Date parsing issues are coding bugs, not infrastructure problems. If Invalid Date is appearing in a live application, a developer needs to add input validation and a date parsing library.

Frequently Asked Questions

Why does my date work in Chrome but show Invalid Date in Safari?

Safari's JavaScript engine is stricter about date formats. It rejects formats like MM/DD/YYYY that Chrome accepts. Always use ISO 8601 format (YYYY-MM-DD) or a parsing library to guarantee cross-browser compatibility.

How do I check if a Date object is valid?

Use: isNaN(myDate.getTime()). If this returns true, the date is invalid. This is the most reliable way to detect Invalid Date in JavaScript.

Can I use new Date() with a Unix timestamp?

Yes — but the timestamp must be a number, not a string. Use new Date(1712000000000) (milliseconds) or new Date(Number(myString)). Passing the string '1712000000000' may work in some browsers but is not guaranteed.