Number of Days Between Two Dates - Problem
You're given two dates in string format as YYYY-MM-DD and need to calculate the absolute number of days between them.
This is a fundamental date calculation problem that appears frequently in real-world applications like subscription billing, project management, and age calculations.
Goal: Calculate the difference in days between two valid dates.
Input: Two date strings in format "YYYY-MM-DD"
Output: A single integer representing the absolute number of days between the dates
Example: Between "2020-01-15" and "2019-12-31", there are 15 days.
Input & Output
example_1.py โ Basic Date Difference
$
Input:
date1 = "2019-06-29", date2 = "2019-06-30"
โบ
Output:
1
๐ก Note:
The difference between June 29, 2019 and June 30, 2019 is exactly 1 day. Simple consecutive date calculation.
example_2.py โ Cross-Year Calculation
$
Input:
date1 = "2020-01-15", date2 = "2019-12-31"
โบ
Output:
15
๐ก Note:
From Dec 31, 2019 to Jan 15, 2020: 15 days total. This tests year boundary crossing and ensures we return absolute difference.
example_3.py โ Same Date Edge Case
$
Input:
date1 = "2020-02-29", date2 = "2020-02-29"
โบ
Output:
0
๐ก Note:
When both dates are identical, the difference is 0. This also tests leap year date handling (Feb 29).
Constraints
- The given dates are valid dates between the years 1971 and 2100
-
Dates are given in format
YYYY-MM-DD - Both dates will be valid according to the Gregorian calendar
- Years from 1971 to 2100 inclusively
Visualization
Tap to expand
Understanding the Visualization
1
Reference Point
Establish year 1 AD as our reference epoch (day 0)
2
Convert First Date
Calculate total days from epoch to first date
3
Convert Second Date
Calculate total days from epoch to second date
4
Find Difference
Subtract the two values and take absolute difference
Key Takeaway
๐ฏ Key Insight: Converting dates to a numerical representation (days since epoch) transforms the problem from iterative counting to simple arithmetic subtraction.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code