-
-
Notifications
You must be signed in to change notification settings - Fork 407
fix: blog post published date #1892
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
JavaScript new Date('invalid').toLocaleDateString() behaviour throw exception or Invalid Date💡 Result:
new Date("invalid")creates a Date object whose internal time value isNaN(an “Invalid Date”).For
toLocaleDateString(), the ECMAScript Internationalization (ECMA‑402) spec says:NaN, return the string"Invalid Date"(no exception). [1]So:
When you do see an exception with date formatting APIs, it’s typically a
RangeErrordue to invalidlocales/options(e.g., an invalidtimeZone), not because the Date is invalid. [1](Separately:
toISOString()does throw on an invalid date, which is a different method and behavior.) [2]Sources: [1] [2]
🌐 Web query:
JavaScript Date.toLocaleDateString() timezone day shift date-only strings timeZone UTC💡 Result:
Date.prototype.toLocaleDateString()formats the same instant in time in whatever time zone you tell it (default: the user’s local time zone). If that instant crosses midnight in that zone, the calendar day can shift. (developer.mozilla.org)Why it happens with date-only strings (
"YYYY-MM-DD")In JavaScript, an ISO date-only string like
"2026-03-03"is parsed as UTC midnight (2026-03-03T00:00:00.000Z). (stackoverflow.com)If you then format it in a negative-offset time zone (e.g., America/Los_Angeles), that UTC midnight is still the previous local evening, so the displayed date becomes the day before. (developer.mozilla.org)
Fixes (pick based on what your string means)
timeZoneis anIntl.DateTimeFormatoption used bytoLocaleDateString. (developer.mozilla.org)Zor-05:00) rather than date-only strings. (stackoverflow.com)Citations:
🏁 Script executed:
Repository: npmx-dev/npmx.dev
Length of output: 705
Stabilise date parsing and timezone to prevent incorrect published dates.
The try/catch fallback is ineffective: invalid dates do not throw exceptions—
toLocaleDateString()returns the string"Invalid Date"instead. Additionally, ISO date-only strings (e.g."2026-03-01") are parsed as UTC midnight and will display as the previous day in western timezones unless formatted withtimeZone: 'UTC'.Proposed fix
export function toLocaleDateString(date: string): string { if (!date) return '' - try { - return new Date(date).toLocaleDateString('en-US', { - year: 'numeric', - month: 'short', - day: 'numeric', - }) - } catch { - return date - } + const parsed = new Date(date) + if (Number.isNaN(parsed.getTime())) return date + + return parsed.toLocaleDateString('en-US', { + year: 'numeric', + month: 'short', + day: 'numeric', + timeZone: 'UTC', + }) }📝 Committable suggestion