TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

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

43 点作者 dsiegel2275超过 1 年前

14 条评论

MrHus超过 1 年前
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 未加载
torstenvl超过 1 年前
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_g超过 1 年前
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 未加载
tzs超过 1 年前
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>
solardev超过 1 年前
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.
avandekleut超过 1 年前
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 未加载
wouldbecouldbe超过 1 年前
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 未加载
RicoElectrico超过 1 年前
Apparently, date handling is still causing problems in 2024, as evidenced by the Newag trains&#x27; DRM.
oleganza超过 1 年前
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.
candiddevmike超过 1 年前
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 未加载
wffurr超过 1 年前
Did I not look closely enough? I didn’t see any tests in the changes.
Pxtl超过 1 年前
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 未加载
cranium超过 1 年前
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.
ramses0超过 1 年前
...the comment! The hubris!<p><pre><code> &#x2F;&#x2F; Important: Important to set these in order</code></pre>
评论 #39205356 未加载