Compute days between dates
To compute days between dates, convert each date to a day number on one scale and subtract. That difference is how many days in between two dates as elapsed midnights.
how many days in between two dates
Computing is the same count as walking, done with day numbers so a long span does not depend on a finger on a page. Give each Gregorian date an ordinal: a year-day, or a serial count of days from a fixed origin. Subtract the earlier ordinal from the later one. That difference is the elapsed days between them.
A year-day is enough when both dates sit in the same year. 30 June 2026 is day 181 of that common year; 1 June 2026 is day 152; 181 minus 152 is 29. Across years, add the unused days of the first year, the full years in the middle, and the year-day of the end. Full years contribute 365 or 366 according to the leap rule, not according to habit.
The origin you pick for a serial number does not matter as long as both dates use it. Software often uses a fixed day zero. You can use 1 January of a convenient year. Consistency is the requirement. Mixing two origins is not a computation; it is two clocks pretending to be one.
Once you have the elapsed figure, the inclusive figure is one more, except when the two dates are the same civil date: elapsed 0, inclusive 1. Do not add one to a count that is already inclusive, and do not add one to a count of nights.
Leap day is a date, not a bonus
A leap day is just a date. If 29 February is strictly after the start and on or before the end, under the elapsed rule, it contributes one. If the span is 28 February to 1 March in 2028, elapsed days are 2. In 2026 that same numbered pair is 1. Compute the pair. Do not compute “leap year” as a blanket extra.
Spans that cover several Februaries need a leap-day census, not a vibe. List the years whose 29 February falls inside the interval. Years divisible by 4 are candidates; drop century years that are not also divisible by 400. 1900 contributes no leap day. 2000 contributes one. 2100 will contribute none.
Test each year whose February sits in the interval, not only the start year and the end year. Ignore leap years whose 29 February lies before the start or after the end. Never add a quarter-day per year and round. That average describes centuries, not a deadline.
The extra fraction in the average Gregorian year is why leap days exist at all. It is the wrong instrument for a span of forty days. Use it only when you are talking about the calendar’s long-run length, not when you are computing two named dates.
If February 29 sits in the interval, it is a day. If it does not, no amount of leap-year talk should add it.
Sign, order, and a second method
Computation has a direction. End minus start is positive when the end is later. Start minus end is negative. If a program always returns a positive integer, it computed distance and threw the sign away. Distance is useful for “how far apart.” Sign is useful for “which one is first.”
Time of day is outside this computation unless you asked for hours. Two timestamps can be 23 or 25 hours apart across a daylight-saving change and still be consecutive dates. If the inputs are dates with no clock, the output is an integer of dates, not a duration in seconds.
When the result matters, recompute with a second method: weekday arithmetic for short spans, or an independent serial. If the two methods disagree, the leap day or the inclusive rule is the usual suspect, not the idea of subtraction.
Questions and answers
- What is the core computation for days between two dates?
- Turn each date into a day number on the same scale, subtract, and then add one only if you need an inclusive count of dates.
- How do I handle a leap day when computing?
- Include it when that date falls inside the span under your endpoint rule. Do not add a leap day merely because the start year or the end year is a leap year if 29 February is outside the span.
- Should a reversed pair return an error?
- Not by default. Return a negative elapsed count, or return a distance and a direction. Silence about order is the error.
Related guides
10 year anniversary
A 10 year anniversary is the same month and day ten civil years after the original date. If that day does not exist in the later year, pick 28 February or 1 March and say which.
10000 days
10,000 days is a little more than 27 years: 27 × 365 = 9,855 days, so 145 days remain if you ignore leap days.
107 days
107 days is a span of 107 midnights on the civil calendar: 15 weeks and 2 days. From Thursday 3 September 2026 that span ends on Saturday 19 December 2026.