Date Math Explained: Days Between Dates, Adding and Subtracting Days
Date arithmetic should be simple. You want to know how many days until your vacation, or what date falls 90 days from today, or how long it’s been since you started a project. Just add and subtract, right? But the moment you actually try to do this with precision, you run into a wall of complications that has bedeviled programmers, lawyers, and accountants for centuries.
We built our date calculator and days between dates tool to handle all of these quirks automatically. But we think it’s worth understanding why date math is hard—and how it works under the hood—because the pitfalls show up in everyday life more often than you’d expect.
Why Date Math Is Harder Than Regular Math
Regular arithmetic is clean. 5 + 3 = 8, always. Date arithmetic is messy because the calendar is irregular in multiple overlapping ways:
- Months have different lengths. January has 31 days, February has 28 (or 29), April has 30. There’s no formula for “how many days are in month X” that doesn’t require a lookup table.
- Leap years are almost but not perfectly periodic. A year is a leap year if it’s divisible by 4, unless it’s divisible by 100, unless it’s also divisible by 400. So 2024 is a leap year, 1900 was not, and 2000 was. This three-layer rule means you can’t just check divisibility by 4.
- Daylight saving time changes the length of certain days. In most of the US, one day per year is 23 hours long (spring forward) and another is 25 hours (fall back). If you’re counting hours between two timestamps, DST transitions can silently shift your result.
- The Gregorian calendar has a messy history. Different countries adopted it at different times, creating gaps in the historical calendar that can confuse date calculations spanning centuries.
Counting Days Between Two Dates
The most common date math operation is: how many days from Date A to Date B? This comes up constantly—tracking how many days until a deadline, computing the length of a rental agreement, or figuring out how long you’ve been at your job.
The Method
The reliable way to count days between dates is to convert each date to a serial day number and subtract. Computers do this using various epoch systems:
- Unix time counts seconds since January 1, 1970 (midnight UTC). To get days, divide by 86,400.
- Julian Day Numbers count days since January 1, 4713 BC. Astronomers invented this system to avoid the headaches of calendar transitions.
- Excel serial dates count days since January 1, 1900 (with a famous bug: Excel incorrectly treats 1900 as a leap year, inherited from Lotus 1-2-3).
Once both dates are serial numbers, subtraction is trivial. The complexity is hidden in the conversion step, which must handle leap years, month lengths, and calendar transitions correctly.
Inclusive vs. Exclusive Counting
Here’s an ambiguity that catches people off guard: how many days from January 1 to January 3?
- Exclusive counting: 2 days (January 1 → January 2 is one day, January 2 → January 3 is two days).
- Inclusive counting: 3 days (January 1, January 2, January 3 are all counted).
Neither answer is “wrong”—it depends on what you’re measuring. If you want to know how many nights you’ll sleep in a hotel checking in January 1 and out January 3, the answer is 2 (exclusive). If you want to know how many days of medicine you need for a regimen spanning January 1 through January 3, the answer is 3 (inclusive).
Legal and financial contexts usually specify which counting method applies. A “90-day return policy” from a purchase on March 1 typically means the last return date is May 30 (90 days later, exclusive of the start date, counting the end date). But “within 30 days” can mean different things depending on jurisdiction.
Adding and Subtracting Days
Adding days to a date is straightforward: convert to a serial number, add the number of days, convert back. What date is 90 days from April 9, 2026? Add 90 to the serial day number for April 9, and you get July 8, 2026. Our date calculator does this instantly.
The tricky part comes when you cross month boundaries and need to account for different month lengths. Adding 30 days from January 15 gets you to February 14. Adding 30 days from January 31 gets you to March 2 (not February 30, which doesn’t exist). The serial-number approach handles this automatically.
The Month-Addition Problem
Adding months is where date math gets genuinely ambiguous. Adding days is deterministic (a day is always a day), but “one month” is not a fixed number of days. Consider: what is one month after January 31?
- Option 1: Clamp to end of month. February 28 (or 29). The logic: go to the same day number in the next month, but if it doesn’t exist, use the last day of that month.
- Option 2: Roll forward. March 3 (or March 2 in a leap year). The logic: January 31 + 31 days = March 3. Treat “one month” as the number of days in the starting month.
- Option 3: Error. February 31 doesn’t exist, so the operation is undefined.
Most date libraries use Option 1 (clamping). PHP, Python’s dateutil, and Java’s LocalDate all clamp to the end of the month. But this creates a mathematical oddity: January 31 + 1 month = February 28, and February 28 + 1 month = March 28. So adding one month and then another month doesn’t give the same result as adding two months at once (January 31 + 2 months = March 31). Month addition is not associative.
This isn’t a bug in any specific calculator—it’s an inherent property of months having different lengths. There is no perfect solution, only trade-offs that different systems make differently.
Business Days vs. Calendar Days
In many real-world scenarios, you don’t want to count all days—you want to count only working days (Monday through Friday), optionally excluding public holidays.
Where the Distinction Matters
- Shipping estimates: “Ships in 3–5 business days” means you might wait 5–7 calendar days (or more if a holiday falls in the range).
- Legal deadlines: Court filings often use business days. “You have 10 business days to respond” skips weekends and court holidays.
- Financial settlements: Stock trades in the US settle on T+1 (one business day after the trade date). Bond trades settle on T+1 or T+2 depending on the instrument.
- Employment: Notice periods are often in business days. “Two weeks’ notice” typically means 10 business days, though some contracts specify calendar days.
Calculating Business Days
To count business days between two dates, you need to:
- Count the total calendar days.
- Subtract the number of weekend days (Saturdays and Sundays) in the range.
- Subtract any public holidays that fall on weekdays.
Step 3 is the hard part. Public holidays vary by country, state, and even city. US federal holidays are different from UK bank holidays. Some holidays fall on fixed dates (July 4, December 25), while others are floating (Thanksgiving: fourth Thursday in November, Easter: varies based on a lunar calculation). A business-day calculator needs a holiday table for the relevant jurisdiction, and that table changes from year to year.
Real-World Date Math Examples
The 90-Day Return Policy
You buy a laptop on February 15, 2026. The store offers a 90-day return window. When is the last day you can return it? Count 90 days from February 15: you land on May 16, 2026. But does “90 days” include the purchase date or not? If the policy says “within 90 days of purchase,” most retailers treat the purchase date as day 0, making the last return date May 16. If it says “90 days including the purchase date,” the last date is May 15.
Pregnancy Due Dates: Naegele’s Rule
Obstetricians have used Naegele’s Rule since the early 1800s to estimate due dates. The formula: take the first day of the last menstrual period (LMP), add one year, subtract three months, and add seven days. If the LMP was June 10, 2025: add a year (June 10, 2026), subtract three months (March 10, 2026), add seven days (March 17, 2026).
This is equivalent to adding 280 days (40 weeks) from the LMP. The method involves crossing year boundaries, navigating months of different lengths, and potentially spanning a leap year. It’s a compact example of why date math requires careful handling—an off-by-one error in a due date calculation could mean scheduling a C-section on the wrong day.
The 30-Day Notice
You give notice to your landlord on January 15. The lease requires 30 days’ notice. Your move-out date is February 14. But what if you give notice on January 31? Thirty days later is March 2 (since February has only 28 days in a common year). This surprises people who expect “one month’s notice” and “30 days’ notice” to be the same thing—they’re not, because months and 30-day periods don’t align.
School Year Length
Many US states require a minimum of 180 school days per year. Administrators must count school days (excluding weekends, holidays, snow days, and professional development days) to ensure compliance. This is a business-day calculation with a custom holiday calendar—one of the more complex practical applications of date math.
How Computers Represent Dates
Unix Timestamps
The most common internal date representation in computing. A Unix timestamp is the number of seconds since midnight UTC on January 1, 1970 (the “Unix epoch”). As of April 9, 2026, the Unix timestamp is approximately 1,775,865,600. This representation makes date arithmetic trivial for computers—adding a day means adding 86,400—but it doesn’t account for leap seconds, and it’s timezone-agnostic, which can cause issues when converting back to local calendar dates.
Julian Day Numbers
Astronomers use Julian Day Numbers (JDN), which count days from a reference point in 4713 BC. This system is completely free of calendar-transition issues because it predates all modern calendars. It’s the gold standard for date difference calculations spanning long historical periods.
ISO 8601
The international standard for date representation: YYYY-MM-DD. It’s unambiguous (unlike 04/09/2026, which could be April 9 or September 4), it sorts lexicographically, and it’s universally supported by programming languages and databases. If you’re storing or transmitting dates, ISO 8601 is the format to use.
The Missing 11 Days of 1752
One of the most bizarre episodes in calendar history: when Britain and its colonies adopted the Gregorian calendar in September 1752, they had to skip 11 days to synchronize with the rest of Europe. Wednesday, September 2, 1752 was followed by Thursday, September 14, 1752. September 3 through September 13 simply did not exist that year.
If you try to run the Unix cal command for September 1752, you’ll see those 11 days missing from the output—a historical artifact preserved in software. Date calculations that span this transition (rare, but possible in genealogy and historical research) must account for the gap, or they’ll be off by 11 days.
Other countries made the switch at different times. Russia didn’t adopt the Gregorian calendar until 1918, which is why the “October Revolution” of 1917 actually took place in November by the Gregorian calendar. Greece held out until 1923. These transitions mean that “what day was it on [historical date] in [country]?” can have different answers depending on which calendar was in use.
Daylight Saving Time and Date Math
If you’re counting days, DST usually doesn’t matter—a day is a calendar day regardless of whether it has 23, 24, or 25 hours. But if you’re counting hours or computing precise timestamps, DST transitions can bite. Adding exactly 24 hours to “2:00 AM on the day clocks spring forward” gives you 3:00 AM the next day, not 2:00 AM. Adding “one day” gives you 2:00 AM. The difference between adding a duration (24 hours) and adding a calendar unit (1 day) matters when DST is involved.
Try It Yourself
Whether you need to find a date 90 days from now, count the days between two milestones, or figure out what day of the week a historical date fell on, our date calculator and days between dates tools handle all the edge cases we’ve covered—leap years, month-length variations, and precise day counting.
Open Date Calculator → Days Between Dates →