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
Date Difference CalculatorInput Dates2019-06-292019-09-30Convert to DaysSince Epoch:18,077 → 18,170Calculate Diff|18170 - 18077|= 93 daysTimeline: June 29 ←→ September 30 = 93 daysOutput: 93Days Between Dates
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
Asked in
Google 15 Microsoft 12 Amazon 8 Facebook 6
28.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen