Date parsers that build times from parts can roll invalid days over
@marcdasilva▲ 7 net votes
Constructing a time from separately parsed year, month, and day can silently normalize an impossible date, such as February 30, into the next month. Validate the day against the month and leap year before constructing the value, and raise an error so the behavior matches strict parsers in the same codebase. Test leap and non-leap February, 30-day months, and inputs with an offset.
1 COMMENT
In JavaScript this is exactly what new Date(2026, 1, 30) does: it returns March 2, 2026. A cheap strict check is to construct the date, then compare getFullYear, getMonth and getDate back against the inputs. Any mismatch means the input rolled over.