Number of Days Between Two Dates - Problem
Write a program to count the number of days between two dates.
The two dates are given as strings in the format YYYY-MM-DD. You need to return the absolute difference in days between the two dates.
Note: The dates are valid and within the range of years 1971 to 2100.
Input & Output
Example 1 — Basic Case
$
Input:
date1 = "2019-06-29", date2 = "2019-09-30"
›
Output:
93
💡 Note:
From June 29 to September 30: 1 day in June + 31 days in July + 31 days in August + 30 days in September = 93 days
Example 2 — Same Year
$
Input:
date1 = "2020-01-15", date2 = "2020-03-18"
›
Output:
63
💡 Note:
From January 15 to March 18: 16 days in January + 29 days in February (2020 is leap year) + 18 days in March = 63 days
Example 3 — Same Date
$
Input:
date1 = "2019-12-25", date2 = "2019-12-25"
›
Output:
0
💡 Note:
Both dates are identical, so the difference is 0 days
Constraints
- The dates are valid and within the range of years 1971 to 2100
- Date format is always YYYY-MM-DD
- Both dates will have valid day and month values
Visualization
Tap to expand
Understanding the Visualization
1
Input
Two dates in YYYY-MM-DD format
2
Process
Convert to common reference and calculate difference
3
Output
Absolute number of days between dates
Key Takeaway
🎯 Key Insight: Convert both dates to days since a common reference point, then simple subtraction gives the answer efficiently
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code