Gap in Days Between Two Calendar Strings Without Date Libraries
Math & Bit ManipulationAuto-graded · 21 test casesLast updated
Parse two ISO-style date strings and compute the absolute day gap using manual Gregorian leap-year logic, without any date or calendar library.
Gap in Days Between Two Calendar Strings
Two calendar dates arrive as text in the form YYYY-MM-DD. Work out how many days separate them and return that count as a non-negative integer.
Your solution must not use any built-in date, time, or calendar facility (for example Python's datetime or calendar modules). All calendar logic has to come from the rules below, which you implement yourself.
Calendar rules
- Month lengths are: January 31, February 28 (29 in a leap year), March 31, April 30, May 31, June 30, July 31, August 31, September 30, October 31, November 30, December 31.
- A year is a leap year if it is divisible by 4, except when it is divisible by 100, unless it is also divisible by 400. Thus 2000 and 2024 are leap years, while 1900 and 2100 are not.
Input
first_date: a string inYYYY-MM-DDformat.second_date: a string inYYYY-MM-DDformat.
Both strings are valid calendar dates. Either one may be the earlier date.
Output
Return one integer: the absolute number of days between the two dates. If both dates are identical, return 0.
Constraints
1900-01-01 <= first_date, second_date <= 2100-12-31- Both inputs are valid dates written exactly as
YYYY-MM-DD. - No date/time library may be used; leap years must be handled using the rules above.
Examples
Example 1
Input: day_gap('2019-06-29', '2019-06-30')
Output: 1
Example 2
Input: day_gap('2019-12-31', '2020-01-15')
Output: 15
Example 3
Input: day_gap('2020-02-28', '2020-03-01')
Output: 2
Example 4
Input: day_gap('2019-02-28', '2019-03-01')
Output: 1
Example 5
Input: day_gap('2021-07-04', '2021-07-04')
Output: 0
Hints
Hint 1
Instead of subtracting year, month and day separately, think about turning each date into a single number: how many days have passed since some fixed starting point.
Hint 2
The starting point cancels out when you subtract, so pick whichever is convenient. For a date Y-M-D, add up the days in all complete years before Y, the days in the complete months before M, and then D - 1.
Hint 3
Count leap years before year Y with n = Y - 1: n // 4 - n // 100 + n // 400. Add the leap day for February only when the month is after February and Y itself is a leap year. Finish with abs().
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Subtracting dates field by field means dealing with borrowing across months and years, and February changes length depending on the year. A simpler route is to convert each date into one integer, its ordinal: the number of days elapsed since a fixed starting date (here 0001-01-01). The answer is then the absolute difference of the two ordinals. The starting point cancels in the subtraction, so its exact choice does not matter.
For a date Y-M-D, the ordinal is the sum of three parts:
- Complete years before
Y. Withn = Y - 1, these contribute365 * ndays plus one extra day per leap year in[1, n]. That count isn // 4 - n // 100 + n // 400(multiples of 4, minus century years, plus years divisible by 400). - Complete months before
Min yearY. Add the standard lengths of months1 .. M-1, plus one more day if February is among them andYis a leap year. - Days before
Din the month. This isD - 1.
Why it is correct
Moving forward one day always raises the ordinal by exactly 1:
- Within a month,
D - 1goes up by 1. - Across a month boundary, the day term resets to 0 and the month term grows by the length of the month just finished, giving a net change of +1.
- Across a year boundary (Dec 31 to Jan 1), the month and day terms reset from 364 (or 365 in a leap year) to 0, while the year term grows by 365 (or 366), again +1.
So the ordinals are consecutive integers for consecutive dates, and their difference is the number of days between the dates. abs handles either input order, and equal dates give 0. The leap day of the current year is added only when February is fully passed (M >= 3), so dates in January and February are not over-counted. Century years are handled correctly: 1900 and 2100 are not leap, 2000 is.
Complexity
- Time: O(1). Parsing is constant, the year part is a closed-form formula, and the month loop runs at most 11 times.
- Space: O(1).
Reference solution
def day_gap(first_date: str, second_date: str) -> int:
month_lengths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def is_leap(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
def to_ordinal(date):
year = int(date[:4])
month = int(date[5:7])
day = int(date[8:10])
prev_years = year - 1
total = 365 * prev_years + prev_years // 4 - prev_years // 100 + prev_years // 400
for m in range(1, month):
total += month_lengths[m - 1]
if m == 2 and is_leap(year):
total += 1
return total + day - 1
return abs(to_ordinal(first_date) - to_ordinal(second_date))