Our great sponsors
-
SurveyJS
Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.
You should never, ever do date math naively like this. There are too many unexpected edge case, especially between time zones or daylight savings time or leap years, but even without them: https://moment.github.io/luxon/#/math
In fact I would strongly argue you should never use the JS Date built-in at all because they are terrible. Use a library like Luxon or date-fns. As a frontend dev, this is the most common category of bugs I've dealt with in my career, and I've spent dozens of hours fixing other people's datetime handling because of how poorly implemented the JS Date API is. It's full of minefields and gotchas.
The Temporal API is supposed to fix some issues, but that's been in development for like a decade now.
Very relatedly, there's a potential similar footgun related to hours, due to the fact that dates initialized without hour default to 00:00 in your local timezone.
It's possible then to change the date, and the time will jump to 23:00 due to timezone DST changes.
Example bug I had in 2014 when Russian timezone definitions have changed:
https://github.com/yui/yui2/pull/15