Skip to content

fix(common): warn in dev mode when toDate() receives an unrecognized string format#68909

Open
arturovt wants to merge 1 commit into
angular:mainfrom
arturovt:fix/common_to_date_warn
Open

fix(common): warn in dev mode when toDate() receives an unrecognized string format#68909
arturovt wants to merge 1 commit into
angular:mainfrom
arturovt:fix/common_to_date_warn

Conversation

@arturovt
Copy link
Copy Markdown
Contributor

Prior to this commit, toDate() silently forwarded unrecognized date strings to new Date() with no indication to the developer that this behavior was unsafe. The result is platform and timezone dependent — a server running in UTC and a browser in UTC+8 will produce different dates from the same input string, causing SSR hydration mismatches.

Example:

// Browser in UTC+8 (offset 480):
new Date('Jan 15 2024').toISOString() // → 2024-01-15T08:00:00.000Z

// Server in UTC (offset 0):
new Date('Jan 15 2024').toISOString() // → 2024-01-15T00:00:00.000Z

The same input string produces a different date depending on where the code runs.

The fix adds a console.error in ngDevMode when toDate() receives a string that does not match any of the supported formats — ISO 8601 date-only, ISO 8601 datetime, or a numeric string. The error message tells the developer exactly which value triggered the warning and how to fix it. Production behavior is unchanged — the new Date() fallback still runs, so no breaking changes are introduced.

@angular-robot angular-robot Bot added the area: common Issues related to APIs in the @angular/common package label May 24, 2026
@ngbot ngbot Bot added this to the Backlog milestone May 24, 2026
@arturovt arturovt marked this pull request as ready for review May 24, 2026 21:33
@pullapprove pullapprove Bot requested a review from JeanMeche May 24, 2026 21:33
…string format

Prior to this commit, `toDate()` silently forwarded unrecognized date strings to `new Date()` with no indication to the developer that this behavior was unsafe. The result is platform and timezone dependent — a server running in UTC and a browser in UTC+8 will produce different dates from the same input string, causing SSR hydration mismatches.

**Example:**

```js
// Browser in UTC+8 (offset 480):
new Date('Jan 15 2024').toISOString() // → 2024-01-15T08:00:00.000Z

// Server in UTC (offset 0):
new Date('Jan 15 2024').toISOString() // → 2024-01-15T00:00:00.000Z
```

The same input string produces a different date depending on where the code runs.

**The fix** adds a `console.error` in `ngDevMode` when `toDate()` receives a string that does not match any of the supported formats — ISO 8601 date-only, ISO 8601 datetime, or a numeric string. The error message tells the developer exactly which value triggered the warning and how to fix it. Production behavior is unchanged — the `new Date()` fallback still runs, so no breaking changes are introduced.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: common Issues related to APIs in the @angular/common package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant