vintage pocket watch, free background, wallpaper 4k, laptop wallpaper, 4k wallpaper, letters, windows wallpaper, wallpaper hd, beautiful wallpaper, pocket watch, mac wallpaper, vintage watch, hd wallpaper, 4k wallpaper 1920x1080, close up, desktop backgrounds, cool backgrounds, background, free wallpaper, full hd wallpaper, dried roses, vintage aesthetic. How to calculate the days between two dates, step by step
Photo by Ylanite on Pixabay

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.

This is an editorial pen name used by the publication, not a claim of an independently verified personal identity. Original editorial credit: Review Desk.

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.
  • NETWORKDAYS counts 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.

  1. If m is January or February, subtract 1 from y and add 12 to m. February is now month 14 of the previous year.
  2. Day of the shifted yeardoy = (153 * (m - 3) + 2) / 5 + d - 1. The 153 and 5 encode the repeating pattern of 31 and 30-day months from March onward.
  3. Position in the 400-year cycleyoe = y mod 400, then doe = yoe * 365 + yoe / 4 - yoe / 100 + doy. The two divisions add a day for each fourth year and take it back for each century.
  4. Day numberera * 146097 + doe, where era = y / 400 and 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.

FromToDay-number differenceDirect count
10 February 202723 November 2027286286
31 January 20271 March 20272929
5 March 20278 March 202733
1 February 20281 March 20282929
28 February 20001 March 200022
28 February 19001 March 190011
28 February 21001 March 210011
1 January 202731 December 2027364364

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
NETWORKDAYS is 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:

FromToHours
13 March 202714 March 202724
14 March 202715 March 202723
6 November 20277 November 202724
7 November 20278 November 202725
1 March 20271 April 2027743

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.

ModeStartEndConventionExpect
datesFri 5 MarchMon 8 Marchcalendar, start left out3
datesFri 5 MarchMon 8 Marchcalendar, both ends in4
datesMon 1 MarchMon 8 Marchworking, both ends in6
datesMon 1 MarchMon 15 Marchworking, both ends in, Wed 10 March closed10
datesSat 6 MarchSun 7 Marchworking, both ends in0
momentsFri 5 March 15:00Mon 8 March 11:00elapsed hours68
momentsFri 12 March 15:00Mon 15 March 11:00elapsed hours, Chicago67
moments14 March 00:0015 March 00:00elapsed hours, Chicago23

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

More in Rules

Latest from Evelyn C. Reed