Guides

Fixing "Invalid Date" Errors When Parsing Timestamps in JavaScript

SyncTonight Team7 min read2 views0 likes

"Invalid Date" is what you get when you print a JavaScript Date object that failed to parse correctly — new Date('garbage input') doesn't throw an error, it silently produces a Date object whose internal value is NaN, which then prints as "Invalid Date" wherever you try to use it. This silent failure is exactly what makes date bugs so painful to track down.

The root cause is almost always an ambiguous or unsupported date string format being passed to new Date() or Date.parse(). JavaScript's built-in date parser reliably supports ISO 8601 format (like '2026-04-09T14:30:00Z') and a handful of other common formats, but its behavior for anything else is technically implementation-defined — meaning different browsers and Node.js versions can parse the exact same non-standard string differently, or fail on it entirely.

A classic example is date strings like '09/04/2026' — is that September 4th or April 9th? JavaScript's default parser assumes US month/day/year ordering, which silently produces the wrong date (not even an error) if your data actually uses day/month/year ordering, common outside the US. This is arguably worse than an outright error, because the code runs without complaint and just produces the wrong date.

Another frequent source is receiving a Unix timestamp as a string and forgetting to convert it to a number first, or mixing up seconds versus milliseconds. new Date() expects milliseconds since the Unix epoch — passing a 10-digit second-based timestamp directly, without multiplying by 1000, produces a valid-looking but wildly wrong date, usually sometime in 1970.

The reliable fix is to never depend on JavaScript's ambiguous string parsing for anything that isn't already in ISO 8601 format. If you're receiving dates from an API you control, standardize on ISO 8601 strings or Unix timestamps in milliseconds — both parse unambiguously and consistently everywhere. If you're stuck parsing an ambiguous format from a source you don't control, parse the year, month, and day manually with string splitting or a regular expression, and construct the Date explicitly with new Date(year, monthIndex, day) rather than trusting the string parser to guess correctly.

For anything beyond simple parsing — time zone conversion, relative time formatting ('3 days ago'), or date arithmetic across daylight saving boundaries — reaching for a small, well-tested library rather than hand-rolling the logic is usually worth it, since date/time edge cases are notoriously easy to get subtly wrong even when the happy path works fine.

When you're not sure whether a timestamp value is in seconds or milliseconds, or want to quickly sanity-check what a given value actually represents, paste it into our Timestamp Converter — it auto-detects the unit and shows you the equivalent date in multiple formats side by side, so you can confirm your assumption before it becomes a bug.

Found this helpful?

SyncTonight's tools and guides are free and always will be. If this post saved you some debugging time, a coffee goes a long way — no pressure, just appreciated.

☕ Buy me a coffee

Keep Reading

Also available

We also build websites.

Need a landing page, a full product site, or a custom web app built? We design and develop those too — same speed and no-nonsense approach you see here. Let us know what you're building.

Landing pagesFull websitesWeb appsSaaS MVPsDashboards
Let's talk about your project