Skip to content

fix: parseJSONDate incorrectly adding 1900 to years 0-99 - #741

Merged
ascott18 merged 2 commits into
mainfrom
ascott18-fix-parse-json-date-year-bug
Jun 30, 2026
Merged

fix: parseJSONDate incorrectly adding 1900 to years 0-99#741
ascott18 merged 2 commits into
mainfrom
ascott18-fix-parse-json-date-year-bug

Conversation

@ascott18

Copy link
Copy Markdown
Collaborator

Why

parseJSONDate was returning the wrong year for dates with a year in the range 0-99. For example, parseJSONDate("0001-01-01", "date") produced Tue Jan 01 1901 instead of year 1. This is because JavaScript's Date constructor (and Date.UTC) interpret a year of 0-99 as 1900 + year.

Approach

After constructing the Date, we now shift the result back by exactly 1900 years, but only when the parsed year falls in the 0-99 range. The shift is relative (getFullYear() - 1900) rather than an absolute setFullYear(parsedYear). This matters because the constructor may normalize the date across a year boundary (e.g. a timezone offset that pushes the UTC instant into the previous year); a relative shift preserves that normalization, whereas an absolute set would clobber it and produce an off-by-one-year result.

The fix is applied consistently across all three relevant branches: date-only, datetime without offset, and datetime with a Z/explicit offset. The time-only branch is unaffected (it already uses the current year by design).

Testing

Coverage was added through the existing convertToModel/mapToModel date-mapping table in model.toModel.spec.ts (runs against both mapping variants), covering:

  • Small years across all three branches (0001-01-01, 0099-06-15, 0001-01-01T14:00:00, 0001-06-15T12:00:00Z)
  • Leap day in a small year (0004-02-29)
  • A normal year whose offset crosses back into the prior year, which must NOT be clobbered (2020-01-01T00:30:00+01:00)
  • A small year whose offset crosses back into year 0, validating the relative shift (0001-01-01T00:30:00+01:00)
  • The year < 100 boundary (0100-01-01), which must be left untouched

All 668 coalesce-vue tests pass and tsc is clean.

ascott18 and others added 2 commits June 29, 2026 14:02
JavaScript's Date constructor (and Date.UTC) interpret years 0-99 as
1900+year. We now shift the result back by exactly 1900 years, but only
for years in that range. The shift is relative (not an absolute setYear)
so that timezone offsets which push the instant across a year boundary
are preserved. Adds coverage via the existing convertToModel/mapToModel
date mapping tests, including leap-day and year-crossing-offset cases.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@ascott18
ascott18 merged commit f8a08ec into main Jun 30, 2026
17 checks passed
@ascott18
ascott18 deleted the ascott18-fix-parse-json-date-year-bug branch June 30, 2026 01:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant