Validate Date-Only Strings Without JavaScript Date Rollover
A report filter accepts 2026-02-31, then silently searches for a date in March. The application constructed a JavaScript Date and treated a finite timestamp as proof that the original calendar date was valid.
For a field that represents only a calendar date, validate its text and calendar components directly. Keep the accepted value as a canonical string through filtering and JSON serialization. This article uses the Gregorian calendar and a deliberately narrow YYYY-MM-DD contract.
Reproduce Date Component Rollover
Run this example in a browser console or Node.js:
The component-based APIs normalize overflowing days. That behavior helps with calendar arithmetic but does not validate the original input. The JavaScript Date reference describes component overflow and the distinction between UTC and local methods.
Replacing the constructor with Date.parse is also insufficient for a strict application contract: it accepts more formats than the one chosen here, and handling impossible day/month combinations can differ between implementations. Explicit validation makes the accepted format independent of that parsing behavior.
Define a Date-Only Contract
Use these rules for this example’s API field:
| Part | Accepted form |
|---|---|
| Year | Exactly four ASCII digits, from 0001 through 9999 |
| Month | Exactly two digits, from 01 through 12 |
| Day | Exactly two digits, valid for that month and year |
| Separators | Two literal hyphens, with no whitespace |
| Time and offset | Neither is present |
Year zero, signed years, timestamps, and unpadded values are outside this contract. An empty optional field should be handled explicitly by the caller; it is not a valid date here. Do not trim or pad a malformed value silently at the API boundary.
The HTML Standard’s date rules likewise describe dates without time-zone information and require a day that exists in its month. Our contract additionally caps the year at four digits; HTML’s date syntax permits longer positive years. Apply Gregorian rules throughout the supported range, rather than attempting to model historical calendar changes.
Validate the Format and Calendar Day
Save this helper as date-only.mjs:
| |
The format check precedes numeric conversion, so Number never sees an empty component, exponent, sign, or whitespace. The length check also prevents a trailing newline from slipping through JavaScript’s regular-expression end anchor.
February has 29 days in years divisible by four, except century years that are not divisible by 400. Thus 2000-02-29 is valid while 1900-02-29 and 2100-02-29 are rejected. The helper returns the original validated string and constructs no timestamp.
Compare Validated Date Ranges as Strings
Save this range helper beside it as date-range.mjs:
For these validated, fixed-width ASCII strings, lexicographic order matches calendar order because the year appears first, followed by the month and day. Equality is accepted, so the range can describe a single day. This comparison does not work reliably with unpadded dates or mixed formats.
Use the returned fields directly in an API payload:
The receiving endpoint must define whether its range includes the end date, then enforce the same validation. These helpers validate dates and ordering; they do not calculate the number of days or enforce a maximum reporting period.
Avoid converting the fields through new Date(value) merely to send them back as dates. Under
ECMAScript’s Date.parse rules
, an offset-free date-only string is interpreted as UTC. Reading it with local getters can produce the previous calendar day in time zones west of UTC. Keeping the string avoids introducing that conversion. Turning a date into an event time requires a separate choice of time and time zone.
Test Leap Years and Strict Input Boundaries
Save this as date-only.test.mjs beside both helpers, then run node date-only.test.mjs:
| |
The early-year cases also guard against accidentally introducing the component constructor’s special handling of years 0 through 99. Run the same tests in different process time zones when integrating the helper: acceptance and returned strings should stay identical. Test the server boundary too, including invalid month-end dates, so a direct request cannot bypass client validation.