
Rules
Part of Days between dates: six honest answers to one simple-looking pair
How to calculate the days between two dates, step by step
A leap-year-proof day number built from the March-first year, tested on 1900 and 2000, reconciled with spreadsheet subtraction and NETWORKDAYS, and used for date-only services.
Every date-difference calculation does the same thing: turn each date into one integer, then subtract. What varies is how the integer is built, and the usual bug is a leap day counted twice or not at all. This page builds the integer without a leap-year branch, tests it on the dates that break other methods, ties it to what a spreadsheet does, and then covers the part that matters when a service does the arithmetic: a plain date and a moment are different things.
What to take away
- Start each year on 1 March. The leap day then sits at the end of the shifted year and cannot move any later date.
- Four steps turn a date into a day number. Subtract two day numbers and you have the exclusive calendar count (the start date is not counted).
- For any date from 1 March 1900, this day number minus the Excel serial is the constant 693,899. That is why plain subtraction of two date cells gives the same answer.
NETWORKDAYScounts both ends, so from a working-day start it exceeds a count that leaves the start out by exactly one.- On clock-change days a local day has 23 or 25 hours. A "days between" built from hours divided by 24 is wrong there. Send plain dates for day counts.
Why the year starts in March
A method that tests for a leap year must decide, for every pair of dates, whether the extra day lies between them. Moving the start of the year to March removes the question. February, and its possible 29th day, becomes the last month of the shifted year, so every date after it is unaffected by whether the year is leap.
The rule being encoded is the Gregorian one. The U.S. Naval Observatory states it as: years divisible by 4 are leap years, except centurial years not divisible by 400. So 1700, 1800, 1900 and 2100 are not leap years, and 1600, 2000 and 2400 are.
Build the day number
Take a date as year y, month m (1 to 12) and day d. Use integer division that rounds down.
- If
mis January or February, subtract 1 fromyand add 12 tom. February is now month 14 of the previous year. - Day of the shifted year
doy = (153 * (m - 3) + 2) / 5 + d - 1. The 153 and 5 encode the repeating pattern of 31 and 30-day months from March onward. - Position in the 400-year cycle
yoe = y mod 400, thendoe = yoe * 365 + yoe / 4 - yoe / 100 + doy. The two divisions add a day for each fourth year and take it back for each century. - Day number
era * 146097 + doe, whereera = y / 400and 146097 is the number of days in 400 Gregorian years.
Nothing in these steps asks whether a year is leap. The century rule lives inside the division terms.
One verified test table
The table below is the single set of test vectors for this page. Each row was recomputed from the construction above and compared with a direct day-by-day count. The first block checks the formula. The second block checks the working-day relation used later.
| From | To | Day-number difference | Direct count |
|---|---|---|---|
| 10 February 2027 | 23 November 2027 | 286 | 286 |
| 31 January 2027 | 1 March 2027 | 29 | 29 |
| 5 March 2027 | 8 March 2027 | 3 | 3 |
| 1 February 2028 | 1 March 2028 | 29 | 29 |
| 28 February 2000 | 1 March 2000 | 2 | 2 |
| 28 February 1900 | 1 March 1900 | 1 | 1 |
| 28 February 2100 | 1 March 2100 | 1 | 1 |
| 1 January 2027 | 31 December 2027 | 364 | 364 |
The century rows are the ones that expose weak methods. 2000 had a 29 February, so two days separate 28 February from 1 March. 1900 and 2100 did not, so only one does. A method that treats every fourth year as leap gets those two rows wrong. When the dates are reversed the result is negative: the sign gives the order and the magnitude is the count.
The Julian day link
Astronomers count days as one running integer. The Naval Observatory describes Julian dates as a continuous count of days and fractions since noon Universal Time on 1 January 4713 BC on the Julian calendar. The day number built above is the same idea with a different starting point: it differs from a Julian day number by a fixed constant, so subtracting two of either gives the same count.
That is the whole reason date differences work in a spreadsheet. Microsoft documents that Excel stores dates as sequential serial numbers, and that 1 January 1900 is serial number 1 by default. For every date from 1 March 1900 onward, the day number above exceeds the Excel serial by 693,899. For example, 1 January 2027 is serial 46388 and day number 740,287. The same offset holds for 1 March 1900 (serial 61) and for 1 January 2000 (serial 36526). Dates before March 1900 break the pattern because Excel treats 1900 as a leap year, a fact Microsoft documents. The settings and worked values are in counting between two dates in Excel. =B2-A2 is therefore the day-number subtraction with the origin chosen for you. If either cell carries a time, wrap both in INT so the fraction does not turn 3 days into 2.9.
Reconcile with NETWORKDAYS
NETWORKDAYS(start, end, [holidays]) returns whole working days between two dates. Microsoft's own worked example, from 1 October 2012 to 1 March 2013, returns 110, then 109 with one holiday and 107 with three. Counting weekdays from 1 October 2012 to 1 March 2013 with both ends included gives exactly 110, so the function includes both ends.
The relation is fixed:
- Start on a working day
NETWORKDAYSis one more than the count that leaves the start out.- Start on a rest day
- the two are equal.
The year gives a clean case. From Friday 1 January to Friday 31 December 2027 the exclusive calendar count is 364 days, which is 52 whole weeks. Leaving the start out, 260 days are working days. NETWORKDAYS adds the Friday start and returns 261, the weekday total of 2027. Why a year totals 260, 261 or 262 is a separate question. The six readings a single pair of dates can carry, such as inclusive and exclusive, are in counting between two dates.
Plain dates and moments in a service
A calendar date has no zone and no time. A moment has both. Differences between them are different quantities. The Temporal design for JavaScript makes the split a type: Temporal.PlainDate is a date with no time and no time zone, while a zoned date-time carries an instant, a zone and a calendar. MDN lists Temporal as limited availability, so whichever language the service uses, copy the separation rather than the library.
Clock-change days
In the United States, daylight saving time begins at 2:00 a.m. on the second Sunday of March and ends at 2:00 a.m. on the first Sunday of November, according to NIST. In 2027 those are Sunday 14 March and Sunday 7 November. Measured between local midnights in a U.S. zone such as America/Chicago:
| From | To | Hours |
|---|---|---|
| 13 March 2027 | 14 March 2027 | 24 |
| 14 March 2027 | 15 March 2027 | 23 |
| 6 November 2027 | 7 November 2027 | 24 |
| 7 November 2027 | 8 November 2027 | 25 |
| 1 March 2027 | 1 April 2027 | 743 |
A service that computes days as hours divided by 24 returns about 30.96 for March and must then round. A service that subtracts plain dates returns 31 with nothing to round. Other countries use other dates.
Zones shift the date itself
2027-03-05T23:30:00-05:00 is 04:30 on 6 March in UTC. 2027-03-06T08:30:00+09:00 is 23:30 on 5 March in UTC. Convert both to UTC and read the date part, and the first moves from Friday to Saturday while the second moves from Saturday to Friday. For a day count, send plain dates already read in the governing zone, or send moments together with the zone that dates should be read in.
Formats
For plain dates use YYYY-MM-DD. For moments use the date-time form with an offset. RFC 3339 defines full-date, and a date-time that always carries either Z or a numeric offset, so the profile has no offset-free moment. The W3C date and time note lists the same shapes. Editorial suggestion: reject a moment with no offset and reject a plain date that carries a time.
A request and response shape
This shape is the desk's own design, not a published standard. Editorial suggestion: echo every input that changes the answer.
request
mode: dates | moments
start: 2027-03-05
end: 2027-03-08
zone: "America/Chicago" (moments only)
unit: calendar | working | hours
start_counts: false
end_counts: true
weekend: "0000011" (working only, Monday to Sunday)
calendar: { id, version } (working only)
response
result: 3
unit: calendar
applied: { mode, zone, start_counts, end_counts, weekend, calendar }
skipped: [ rest days and closures inside the span ]
The applied block is the answer's provenance. The skipped list lets a reader check a working-day count by eye. If the start is later than the end, return a negative count or an error and say which in the documentation; do not swap the arguments silently.
Service test vectors
All dates in 2027, Saturday and Sunday as rest days, no closures unless stated.
| Mode | Start | End | Convention | Expect |
|---|---|---|---|---|
| dates | Fri 5 March | Mon 8 March | calendar, start left out | 3 |
| dates | Fri 5 March | Mon 8 March | calendar, both ends in | 4 |
| dates | Mon 1 March | Mon 8 March | working, both ends in | 6 |
| dates | Mon 1 March | Mon 15 March | working, both ends in, Wed 10 March closed | 10 |
| dates | Sat 6 March | Sun 7 March | working, both ends in | 0 |
| moments | Fri 5 March 15:00 | Mon 8 March 11:00 | elapsed hours | 68 |
| moments | Fri 12 March 15:00 | Mon 15 March 11:00 | elapsed hours, Chicago | 67 |
| moments | 14 March 00:00 | 15 March 00:00 | elapsed hours, Chicago | 23 |
The sixth and seventh rows show the same wall-clock gap giving 68 and 67 hours, because the second crosses the March clock change. An implementation returning 24 on the last row is working in a fixed offset rather than a zone. One that returns 2.83 or 3 for the first moment row has merged the two types.
Other working weeks and closure lists change these answers: see counting across borders. Moving forward by working days is in adding business days. How a sheet and a service should agree is in spreadsheet and API tools.
Sources
- [U.S. Naval Observatory
- Leap years](https://aa.usno.navy.mil/faq/leap_years)
- [U.S. Naval Observatory
- Julian date converter and definition](https://aa.usno.navy.mil/data/JulianDate)
- [Microsoft
- DATE function](https://support.microsoft.com/en-us/office/date-function-e36c0c8c-4104-49da-ab83-82328b832349)
- [Microsoft
- NETWORKDAYS function](https://support.microsoft.com/en-us/office/networkdays-function-48e717bf-a7a3-495f-969e-5005e3eb18e7)
- [Microsoft
- Excel incorrectly assumes that the year 1900 is a leap year](https://learn.microsoft.com/en-us/troubleshoot/microsoft-365-apps/excel/wrongly-assumes-1900-is-leap-year)
- [NIST
- Daylight saving time](https://www.nist.gov/pml/time-and-frequency-division/popular-links/daylight-saving-time-dst)
- [IETF RFC 3339
- Date and time on the Internet](https://www.rfc-editor.org/rfc/rfc3339)
- [W3C
- Date and time formats](https://www.w3.org/TR/NOTE-datetime)
- [MDN
- Temporal.PlainDate](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDate)







