TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Fix date-handling bug when today’s date is later than the target month

43 pointsby dsiegel2275over 1 year ago

14 comments

MrHusover 1 year ago
I was just browsing HN because I'm procrastinating solving a date based bug. It is literally this bug! Who said surfing HN is not productive!
评论 #39203870 未加载
torstenvlover 1 year ago
How is 3+3+2+3+2+3+3+2+3+2+3 equal to 5?<p>The bug exists every time today&#x27;s date is greater than the lowest total number of days in a month (28). So it&#x27;s a bug for 3 days in January, (sometimes 1 day in February,) 3 days in March, 2 days in April, etc.
评论 #39203694 未加载
jakub_gover 1 year ago
Very relatedly, there&#x27;s a potential similar footgun related to <i>hours</i>, due to the fact that dates initialized without hour default to 00:00, and that JS dates are in user&#x27;s <i>current local timezone</i>.<p>It&#x27;s possible then that after changing the month programmatically, the time component will jump to 23:00 due to timezone DST changes, and the date will jump back one day as well due to this (and in an unlucky case, the month will jump back as well!)<p>Example bug I had in 2014 when Russian timezone definitions have changed:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;yui&#x2F;yui2&#x2F;pull&#x2F;15">https:&#x2F;&#x2F;github.com&#x2F;yui&#x2F;yui2&#x2F;pull&#x2F;15</a><p>A workaround is to initialize dates with fake &quot;midday&quot; time component so that you avoid the day jumping back.
评论 #39206992 未加载
tzsover 1 year ago
The fix is to change things like this:<p><pre><code> targetEndDate = new Date(); targetEndDate.setFullYear( endDate.getFullYear() ); targetEndDate.setMonth( endDate.getMonth() ); targetEndDate.setDate( endDate.getDate() ); </code></pre> to<p><pre><code> targetEndDate = new Date(); targetEndDate.setFullYear( endDate.getFullYear(), endDate.getMonth(), endDate.getDate()); </code></pre> That works because setFullYear has a three argument form that takes the year, month, and date avoiding the inconsistency that can arise by setting those one at a time.<p>But you know what else has a three argument for that take the year, month, and date? The Date constructor.<p>So why not fix it like this?<p><pre><code> targetEndDate = new Date( endDate.getFullYear(), endDate.getMonth(), endDate.getDate()); </code></pre> Using the empty constructor would initial the object with the current date and time, but they promptly overwrite those.<p>The Date construction also has a form that takes another Date object, so I wonder if they could have simple used:<p><pre><code> targetEndDate = new Date(endDate);</code></pre>
solardevover 1 year ago
You should never, ever do date math naively like this. There are too many unexpected edge cases, especially between time zones or daylight savings time or leap years, but even without them: <a href="https:&#x2F;&#x2F;moment.github.io&#x2F;luxon&#x2F;#&#x2F;math" rel="nofollow">https:&#x2F;&#x2F;moment.github.io&#x2F;luxon&#x2F;#&#x2F;math</a><p>In fact I would strongly argue you should never use the JS Date built-ins 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&#x27;ve dealt with in my career, and I&#x27;ve spent dozens of hours fixing other people&#x27;s datetime handling because of how poorly implemented the JS Date API is. It&#x27;s full of minefields and gotchas.<p>The Temporal API is supposed to fix some issues, but that&#x27;s been in development for like a decade now.
avandekleutover 1 year ago
Reminder to use something like the date-fns library for immutable operations on dates. The native JS date library can be painful to work on when doing operations like adding or subtracting time.
评论 #39203496 未加载
评论 #39203812 未加载
评论 #39203517 未加载
wouldbecouldbeover 1 year ago
I make this bug in my taxes every now and then. Most invoices are from EU, but the few US ones have the months in wrong order, I only notice if the days exceed 12
评论 #39203627 未加载
RicoElectricoover 1 year ago
Apparently, date handling is still causing problems in 2024, as evidenced by the Newag trains&#x27; DRM.
oleganzaover 1 year ago
Unlike many issues with dates, this one is purely a result of a wrong API design. It does not make sense to &quot;set a component&quot; of a date all by itself (unlike hours&#x2F;minutes&#x2F;seconds in a day), so an API that allows you to so is plain broken.<p>Correct API would be to set the components D,M,Y all at once and have them validated on the spot before returning a valid Date value.
candiddevmikeover 1 year ago
I had something like this for a while. It wasn&#x27;t a bug per-say, it was a test flake due to dates and follow on calculations based on them. On the plus side, it prevented you from working on the weekend as the tests&#x2F;PRs would always fail during that time.
评论 #39203716 未加载
评论 #39204081 未加载
wffurrover 1 year ago
Did I not look closely enough? I didn’t see any tests in the changes.
Pxtlover 1 year ago
I&#x27;ve seen somebody write this exact bug before! Hah. Like, something to the effect of<p><pre><code> var newDate = new Date(GetDate().Year, GetDate().Month + 1, GetDate().Day); </code></pre> but with extra logic to ensure that the Month component wrapped around.
评论 #39205780 未加载
craniumover 1 year ago
I tried to replicate the bug only to find out the months are 0-indexed (jan = 0) so the example should use `setMonth(1)`.<p>I must say I&#x27;d prefer getting an exception rather than a silent roll-over any day of the week – pun intended.
ramses0over 1 year ago
...the comment! The hubris!<p><pre><code> &#x2F;&#x2F; Important: Important to set these in order</code></pre>
评论 #39205356 未加载