惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
博客园 - 【当耐特】
M
MIT News - Artificial intelligence
月光博客
月光博客
博客园_首页
博客园 - 叶小钗
T
Tailwind CSS Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
InfoQ
量子位
小众软件
小众软件
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
IT之家
IT之家
Jina AI
Jina AI
阮一峰的网络日志
阮一峰的网络日志
G
Google Developers Blog
WordPress大学
WordPress大学
人人都是产品经理
人人都是产品经理
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks
云风的 BLOG
云风的 BLOG
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
How to Calculate Business Days Between Two Dates in JavaS...
Sabita Kumar · 2026-05-01 · via DEV Community

When working with dates in JavaScript, calculating the difference between two dates is easy if you only need calendar days.

But in real business use cases, we often need to calculate business days, also called working days.

Business days usually exclude Saturdays and Sundays. In some cases, public holidays also need to be excluded.

I created a free online Business Days Calculator for checking this quickly:

https://greatuptools.com/business-days-calculator

What are business days?

Business days usually mean Monday to Friday.

Saturday and Sunday are normally excluded because they are weekends.

For example:

Business days: Monday, Tuesday, Wednesday, Thursday, Friday

Weekend days: Saturday, Sunday

Basic JavaScript date difference

If you only want the number of calendar days between two dates, you can subtract two dates in JavaScript:

Code example:

const startDate = new Date("2026-01-01");
const endDate = new Date("2026-12-31");

const millisecondsPerDay = 1000 * 60 * 60 * 24;
const differenceInDays = Math.floor((endDate - startDate) / millisecondsPerDay);

console.log(differenceInDays);

This counts calendar days, not business days.

Calculate business days in JavaScript

To calculate business days, we can loop through each date and skip Saturday and Sunday.

Code example:

function getBusinessDays(startDate, endDate) {
const start = new Date(startDate);
const end = new Date(endDate);

if (end < start) {
return 0;
}

let count = 0;
const current = new Date(start);

while (current <= end) {
const day = current.getDay();

// 0 = Sunday, 6 = Saturday
if (day !== 0 && day !== 6) {
  count++;
}

current.setDate(current.getDate() + 1);

Enter fullscreen mode Exit fullscreen mode

}

return count;
}

console.log(getBusinessDays("2026-01-01", "2026-12-31"));

How the logic works

JavaScript's getDay() method returns a number from 0 to 6.

0 = Sunday

1 = Monday

2 = Tuesday

3 = Wednesday

4 = Thursday

5 = Friday

6 = Saturday

So the logic is simple:

Code example:

if (day !== 0 && day !== 6) {
count++;
}

This means we count only Monday to Friday.

Inclusive vs exclusive date counting

The above example includes both the start date and the end date.

For example, Monday to Friday gives:

Monday = 1

Tuesday = 2

Wednesday = 3

Thursday = 4

Friday = 5

So the result is 5 business days.

In some cases, you may want to exclude the start date. In that case, you can start counting from the next day.

Business days with public holidays

In real applications, weekends are not the only thing to exclude.

Sometimes you also need to exclude public holidays.

For example:

Code example:

const holidays = [
"2026-01-26",
"2026-08-15",
"2026-10-02"
];

Then you can update the function:

Code example:

function getBusinessDaysExcludingHolidays(startDate, endDate, holidays = []) {
const start = new Date(startDate);
const end = new Date(endDate);

if (end < start) {
return 0;
}

const holidaySet = new Set(holidays);
let count = 0;
const current = new Date(start);

while (current <= end) {
const day = current.getDay();
const dateString = current.toISOString().split("T")[0];

const isWeekend = day === 0 || day === 6;
const isHoliday = holidaySet.has(dateString);

if (!isWeekend && !isHoliday) {
  count++;
}

current.setDate(current.getDate() + 1);

Enter fullscreen mode Exit fullscreen mode

}

return count;
}

console.log(
getBusinessDaysExcludingHolidays("2026-01-01", "2026-12-31", [
"2026-01-26",
"2026-08-15",
"2026-10-02"
])
);

Common use cases

Business day calculation is useful for:

  • Project deadlines
  • Invoice due dates
  • Delivery estimates
  • SLA tracking
  • HR leave calculation
  • Payroll
  • Banking and finance timelines

Try it online

If you do not want to manually calculate business days every time, I built a free online calculator here:

https://greatuptools.com/business-days-calculator

It supports date ranges, weekends, countries, holidays, and result sharing.

Final thoughts

Calculating calendar days is simple in JavaScript, but business day calculation needs extra logic.

The basic rule is:

  • Count Monday to Friday
  • Skip Saturday and Sunday
  • Optionally exclude public holidays

For quick manual checking, you can use this free Business Days Calculator:

https://greatuptools.com/business-days-calculator