JavaScript Date Parsing Bug Can Shift Dates by a Day Across Timezones
A developer building a letter-generation tool discovered that JavaScript's new Date() function parses ISO date strings like '2026-03-01' as UTC, then renders them in the user's local timezone, causing dates to shift by one day for users in time zones behind UTC. For example, a user in California selecting 1 March 2026 would see 28 February 2026 printed in their letter, with no error or warning thrown. The bug also affects invalid dates such as 30 February, which JavaScript silently rolls forward to a valid date rather than returning an error. Adding days using millisecond arithmetic can similarly produce wrong results across daylight saving time boundaries. The fix involves explicitly parsing date components with a regex, constructing dates using local time via new Date(year, month, day), and validating the result by comparing constructed fields back against the original input.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.


Discussion (0)
Log in to join the discussion and vote.
Log in