Watch 9 video solutions for Number of Days Between Two Dates, a easy level problem involving Math, String. This walkthrough by TechZoo has 1,548 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.Problem Overview: You receive two dates in YYYY-MM-DD format and must return the absolute number of days between them. The task is essentially converting both dates into comparable day counts and computing the difference.
Approach 1: Using Built-in Date Libraries (O(1) time, O(1) space)
Most languages provide date utilities that already handle calendar rules such as leap years and month lengths. Parse both strings into date objects using built-in libraries like datetime in Python, LocalDate in Java, or Date in JavaScript. Subtract the two date objects to obtain the difference in days, then return the absolute value. The heavy lifting—parsing, leap-year handling, and calendar arithmetic—is handled internally by the library. This approach is short, reliable, and preferred in production code.
The algorithm simply performs parsing followed by a subtraction operation. Time complexity is O(1) because the input size is fixed and the date arithmetic runs in constant time. Space complexity is also O(1). This approach fits well when standard libraries are allowed and reduces the risk of calendar-related bugs.
Approach 2: Manual Date Calculation (O(1) time, O(1) space)
If library utilities are restricted, compute the difference manually. Split each date string into year, month, and day components. Convert each date into the total number of days since a fixed reference (for example, 1971-01-01 or 0000-01-01). The conversion works by adding days from previous years, accounting for leap years, then adding days from the months in the current year, and finally the day of the month.
Leap years follow the rule: divisible by 4 but not by 100 unless also divisible by 400. Use an array representing days in each month and add one extra day to February during leap years. After converting both dates into total day counts, subtract them and take the absolute value. This approach demonstrates understanding of calendar arithmetic and is commonly expected in coding interviews that test math reasoning and string parsing.
Manual conversion still runs in O(1) time since the number of months and years processed is bounded. Space complexity remains O(1). The complexity appears constant because the input format is fixed length.
Recommended for interviews: Start by explaining the manual conversion approach. It shows you understand leap years, month lengths, and how to convert a date into a numeric representation. After that, mention that real-world code typically relies on built-in date libraries for reliability and readability. Demonstrating both approaches shows strong problem-solving fundamentals and practical engineering judgment.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Using Built-in Date Libraries | O(1) | O(1) | Best for production code when language date utilities are allowed |
| Manual Date Calculation | O(1) | O(1) | When libraries are restricted or interviews test calendar math understanding |