Count Days Between Two Dates
Math & Bit ManipulationAuto-graded · 22 test casesLast updated
Tests efficient calendar arithmetic by computing date intervals without iteration, requiring leap year logic and modular reasoning.
Compute the absolute difference in days between two dates in the Gregorian calendar. Each date is provided as a list or tuple containing three integers: [year, month, day].
Your implementation must run in constant time relative to the span between dates—you cannot loop through each individual day or year. The input may include years up to 10,000,000.
Leap Year Rules
A year qualifies as a leap year when:
- It is evenly divisible by 4, AND
- Either it is not evenly divisible by 100, OR it is evenly divisible by 400.
Examples: 2000 and 2020 are leap years. 1900 and 2100 are not.
Constraints
1 ≤ year ≤ 10^71 ≤ month ≤ 121 ≤ day ≤valid day count for the given month and year- All inputs represent valid Gregorian calendar dates
- Return the non-negative difference regardless of argument order
Examples
Example 1
Input: days_between([2020, 1, 1], [2021, 1, 1])
Output: 366
Example 2
Input: days_between([2000, 1, 1], [2001, 1, 1])
Output: 366
Example 3
Input: days_between([2021, 1, 1], [2020, 1, 1])
Output: 366
Example 4
Input: days_between([2020, 2, 28], [2020, 3, 1])
Output: 2
Hints
Hint 1
Convert each date into a single integer representing days since a fixed starting point, then find the absolute difference between these integers.
Hint 2
Count days contributed by complete years before the target year using inclusion-exclusion for leap years: add multiples of 4, subtract multiples of 100, add back multiples of 400.
Hint 3
For months within the current year, use cumulative day counts. Remember to add one extra day if the date falls after February in a leap year.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Instead of counting days directly between the two dates, we convert each date into a day number—the count of days from a reference epoch (day 1 is January 1, year 1). The answer is then the absolute difference of these two day numbers.
To compute the day number for a date (year, month, day):
-
Count days in all years prior to the current year. There are
year - 1complete years. Each contributes 365 days, plus one additional day for each leap year among them. Using inclusion-exclusion on the range1toyear - 1:- Add
(year - 1) // 4for years divisible by 4 - Subtract
(year - 1) // 100for century years (which are typically not leap years) - Add back
(year - 1) // 400for centuries that are leap years
- Add
-
Count days in complete months before the current month. Use a lookup table of cumulative days for a non-leap year:
(0, 31, 59, 90, ...). If we are past February (month > 2) and the current year is a leap year, add one more day to account for February 29. -
Add the day of the month itself.
Once we have both day numbers, their absolute difference gives the total days between the dates.
Why It Works
The leap year inclusion-exclusion formula precisely captures the Gregorian rules:
- Every fourth year is a candidate leap year
- Century years (divisible by 100) break the pattern unless they are also divisible by 400
- This correctly identifies 2000 as a leap year and 1900 as a common year
We use year - 1 because we are counting only fully elapsed years—whether the current year is a leap year only affects dates within that year, handled separately when adding month offsets.
The day number strictly increases with each calendar day, so subtraction gives the exact count of day boundaries crossed.
Complexity
Time: O(1). Each conversion performs a fixed number of arithmetic operations, independent of the date range.
Space: O(1). Only a constant-size lookup table is used.
Note: Day numbers for years near 10^7 exceed 32-bit integer limits; Python handles this natively with arbitrary-precision integers.
Solution
def days_between(date1, date2):
def is_leap(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
def to_day_number(date):
year, month, day = date
# Count days from all complete years before this one
total = 365 * (year - 1)
total += (year - 1) // 4
total -= (year - 1) // 100
total += (year - 1) // 400
# Add days from complete months in the current year
cumulative_days = (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334)
total += cumulative_days[month - 1]
# Adjust for leap day if past February in a leap year
if month > 2 and is_leap(year):
total += 1
# Add the day within the current month
total += day
return total
return abs(to_day_number(date1) - to_day_number(date2))