Inclusive or exclusive: decide before you count
The difference between two dates depends on whether both endpoints are counted. From 1 March to 31 March, the exclusive answer is 30 days, because the interval contains the 30 nights between the two dates. The inclusive answer is 31 days, because both the first and the last day are counted. A hotel booking, a project duration and a countdown can all use different conventions, so the first step is to state which one you mean.
Time of day adds a second ambiguity. If you subtract two timestamps that include hours and minutes, a 09:00 to 17:00 span is 0.33 of a day, not one day. Date-only arithmetic should discard the time component first, usually by working at UTC midnight, so the result is an integer count of calendar days.
Example: 2025-03-01 to 2025-03-31 is 30 days exclusive and 31 days inclusive. Converting 30 days to weeks gives 4 weeks and 2 days.
Leap years and the Gregorian rule
Not every fourth year is a leap year. The Gregorian rule is: divisible by 4, except centuries, which must be divisible by 400. So 2000 was a leap year, 1900 was not, and 2100 will not be. February has 29 days only when the rule passes.
The difference shows up immediately in date math. From 2024-02-28 to 2024-03-01 is 2 days, because 29 February exists; the same dates in 2025 are 1 day apart. A full year from 2024-01-01 to 2025-01-01 is 366 days. Adding one year to 2024-02-29 cannot produce 29 February 2025, so the result must be clamped to 2025-02-28.
Adding months: the end-of-month problem
Months have different lengths, so "one month later" is not a fixed number of days. The convention that most calendar systems use is to keep the day number and clamp it to the last day of the target month. Adding one month to 2025-01-31 gives 2025-02-28, not 3 March. Adding one month to 2024-01-31 gives 2024-02-29, and adding one month to 2025-03-31 gives 2025-04-30.
Some programming languages do not clamp by default. JavaScript's new Date(2025, 1, 31) rolls over into March, producing 3 March 2025. If you add months by setting the month field and letting the engine normalise, end-of-month dates silently jump into the next month. Clamp explicitly: compute the target month, find its last day, then use the smaller of the original day and that last day.
The order of mixed operations matters too. Starting from 2025-01-30, adding one month and then two days gives 2 March, while adding two days and then one month gives 1 March. Neither is wrong; they answer different questions. Define the order in the specification before writing the code.
Daylight saving makes some days 23 or 25 hours long
A calendar day is usually 24 hours, but not always. In America/New_York, 2025-03-09 lasts 23 hours because the clock jumps from 02:00 to 03:00, and 2025-11-02 lasts 25 hours because 01:00 to 02:00 repeats. Subtract two local timestamps across that boundary and divide by 86,400,000 milliseconds, and you get 0.958 or 1.042 days instead of 1.
Example: 2025-03-08 12:00 to 2025-03-09 12:00 local time in New York is 23 hours, not 24. The fix is to do date arithmetic in UTC, or to count calendar days rather than elapsed hours. Never assume that a local day contains 86,400 seconds.
Working days, weekends and holidays
Subtracting two dates does not tell you how many business days lie between them. Weekends are not universal: much of the Gulf region uses Friday and Saturday, some countries use only Sunday, and a few use a half day. Public holidays vary by country, region and year, and some move with lunar calendars.
The reliable approach is to walk the calendar one day at a time, skip the weekend days defined for that locale, and skip a holiday list. Dividing the total days by 7 and subtracting two weekends is an approximation that fails whenever the interval starts or ends mid-week. Example: ten working days from Monday 2025-06-02, skipping two weekends, ends on 2025-06-16.
Ambiguous date formats
01/02/03 can mean 2 January 2003 in US usage (month/day/year), 1 February 2003 in UK and much of Europe (day/month/year), or 3 February 2001 under a two-digit year/month/day reading. No parser can resolve it without knowing the convention. Store and exchange dates as ISO 8601 YYYY-MM-DD, and show month names in interfaces where clarity matters. Two-digit years also pivot differently across systems, so avoid them entirely in data files.
Another edge case is the missing day: 2025-02-30 and 2025-04-31 do not exist. A validator should reject them rather than silently rolling forward, because a rolled date hides the bad input that caused it.
Checklist before trusting a result
- State inclusive or exclusive. Write it in the label, not only in the code.
- Remove the time component before counting days. Use UTC midnight for date-only arithmetic.
- Clamp month and year additions. Never let 31 January roll into March.
- Expect 23- and 25-hour days. Count calendar days or work in UTC.
- Walk working days. Do not approximate with division when holidays or regional weekends matter.
- Use ISO 8601 in storage. Keep ambiguous slash formats for human conversation only.
Calculate the interval between two dates, or add and subtract days, months and years, with our date calculator. It reports the weekday and handles end-of-month clamping. To understand how dates are stored as numbers, read Unix timestamps: seconds vs milliseconds; for recurring schedules, see cron expressions explained.