<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: シムツール</title>
    <description>The latest articles on DEV Community by シムツール (@_e8d5594dcf13c67bfb1).</description>
    <link>https://dev.to/_e8d5594dcf13c67bfb1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3985956%2Fcebf50c4-b346-4730-8505-2b6e4e949191.png</url>
      <title>DEV Community: シムツール</title>
      <link>https://dev.to/_e8d5594dcf13c67bfb1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/_e8d5594dcf13c67bfb1"/>
    <language>en</language>
    <item>
      <title>Counting business days between two dates (without a library)</title>
      <dc:creator>シムツール</dc:creator>
      <pubDate>Mon, 15 Jun 2026 17:02:37 +0000</pubDate>
      <link>https://dev.to/_e8d5594dcf13c67bfb1/counting-business-days-between-two-dates-without-a-library-3641</link>
      <guid>https://dev.to/_e8d5594dcf13c67bfb1/counting-business-days-between-two-dates-without-a-library-3641</guid>
      <description>&lt;p&gt;"How many working days until the deadline?" sounds trivial until you actually code it.&lt;br&gt;
Calendar dates are full of edge cases. Here's a clean way to count business days in plain&lt;br&gt;
JavaScript, plus the gotchas to watch for.&lt;/p&gt;
&lt;h2&gt;
  
  
  The naive version
&lt;/h2&gt;

&lt;p&gt;Business days = all days between two dates, minus weekends. The simplest correct approach is&lt;br&gt;
to walk day by day and count the ones that aren't Saturday or Sunday:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;businessDaysBetween&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nf"&gt;businessDaysBetween&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// handle reversed range&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;day&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getDay&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;        &lt;span class="c1"&gt;// 0 = Sun, 6 = Sat&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;day&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;day&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setDate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getDate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Walking day-by-day is O(n) in the number of days, which is totally fine for human-scale ranges&lt;br&gt;
(years, not millennia). It's also far easier to get right than clever modular-arithmetic formulas.&lt;/p&gt;

&lt;h2&gt;
  
  
  The gotchas
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Inclusive vs exclusive endpoints.&lt;/strong&gt; Decide whether both the start and end date count.&lt;br&gt;
The loop above is inclusive of both ends. If you want "days remaining," you usually exclude&lt;br&gt;
today (start from tomorrow).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Time zones / DST.&lt;/strong&gt; If you build dates from user input, keep them at a fixed time (e.g. local&lt;br&gt;
midnight) and avoid mixing UTC and local accessors. Iterating with &lt;code&gt;setDate(+1)&lt;/code&gt; is DST-safe&lt;br&gt;
because it operates on the calendar date, not on a fixed number of milliseconds — don't replace it&lt;br&gt;
with &lt;code&gt;+ 86400000&lt;/code&gt;, which can drift across DST boundaries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Holidays.&lt;/strong&gt; Weekends are universal; holidays are not. They depend on country, region, and year.&lt;br&gt;
A weekends-only calculation is honest and predictable; bolting on a holiday list means picking a&lt;br&gt;
locale and keeping it updated. Be explicit about which one you're doing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Reversed ranges.&lt;/strong&gt; Users will enter the end before the start. Decide whether that's an error&lt;br&gt;
or a signed (negative) result, and handle it deliberately.&lt;/p&gt;

&lt;h2&gt;
  
  
  When you just need the answer
&lt;/h2&gt;

&lt;p&gt;If you don't want to wire this up yourself, I built a small set of date tools that do the&lt;br&gt;
day-difference, add/subtract, age, and business-day calculations in the browser (no signup,&lt;br&gt;
nothing sent to a server):&lt;/p&gt;

&lt;p&gt;👉 &lt;strong&gt;Date calculators: &lt;a href="https://date.simtool.dev/" rel="noopener noreferrer"&gt;https://date.simtool.dev/&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The business-days tool counts inclusively and removes weekends only (no holiday list), which keeps&lt;br&gt;
the result predictable across regions — matching the approach above.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaway
&lt;/h2&gt;

&lt;p&gt;For business-day math, a day-by-day walk is simpler and more correct than a formula. Nail down&lt;br&gt;
inclusivity, keep date iteration DST-safe, and be explicit about holidays.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Disclosure: the linked date tools are a free project I built. Sharing it because it's the same&lt;br&gt;
logic described here.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
