Write a program to count the number of days between two dates.
The two dates are given as strings, their format is YYYY-MM-DD as shown in the examples.
Example 1:
Input: date1 = "2019-06-29", date2 = "2019-06-30" Output: 1
Example 2:
Input: date1 = "2020-01-15", date2 = "2019-12-31" Output: 15
Constraints:
1971 and 2100.In LeetCode #1360: Number of Days Between Two Dates, the goal is to compute the absolute number of days between two given dates formatted as YYYY-MM-DD. Since the dates are provided as strings, the first step is to parse the year, month, and day components.
A common approach is to convert each date into the total number of days since a fixed reference point (for example, January 1, 1971). This involves calculating days contributed by complete years, handling leap years, and adding the days from completed months in the current year. Once both dates are converted into total day counts, simply compute the absolute difference.
This method relies on basic math and string processing without requiring advanced data structures. Careful handling of leap years using conditions like (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ensures correctness. The algorithm runs in constant time because the number of months and years processed is bounded.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Convert each date to total days since reference date | O(1) | O(1) |
G Tech Skills
Use these hints if you're stuck. Try solving on your own first.
Create a function f(date) that counts the number of days from 1900-01-01 to date. How can we calculate the answer ?
The answer is just |f(date1) - f(date2)|.
How to construct f(date) ?
For each year from 1900 to year - 1 sum up 365 or 366 in case of leap years. Then sum up for each month the number of days, consider the case when the current year is leap, finally sum up the days.
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Problems involving date calculations, leap year handling, and string parsing are common in coding interviews. While this exact problem may not always appear, similar logic-based date problems are frequently used to test attention to detail and edge case handling.
Leap years are handled using the standard rule: a year is a leap year if it is divisible by 4 but not by 100, unless it is also divisible by 400. When calculating the total days, February should be treated as having 29 days during leap years.
No complex data structures are required for this problem. Simple arrays or lists to store the number of days in each month and basic string parsing are sufficient to compute the total day count.
The optimal approach converts each date into the total number of days since a fixed reference date and then calculates the absolute difference. This method handles leap years and month lengths carefully, ensuring accuracy with constant time complexity.