{x}
blog image

Number of Days Between Two Dates

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:

  • The given dates are valid dates between the years 1971 and 2100.

Solution Explanation: Number of Days Between Two Dates

This problem requires calculating the number of days between two given dates in "YYYY-MM-DD" format. The solution uses a mathematical approach, avoiding the need for external date libraries.

Approach

The core idea is to calculate the number of days from a reference date (e.g., 1971-01-01) to each of the input dates and then find the absolute difference between these two counts. This avoids dealing directly with the complexities of month lengths and leap years in the difference calculation.

The solution comprises several helper functions:

  1. isLeapYear(year): This function checks if a given year is a leap year using the standard leap year rules.

  2. daysInMonth(year, month): This function calculates the number of days in a given month considering leap years (February). It uses an array to store the number of days for each month and adjusts February's count accordingly.

  3. calcDays(date): This is the most crucial function. It takes a date string ("YYYY-MM-DD") as input and calculates the total number of days from 1971-01-01 to the given date. It does this by:

    • Iterating through the years from 1971 to the input year, adding 365 days for each year and an extra day if it's a leap year.
    • Iterating through the months from January to the input month, adding the number of days in each month using daysInMonth.
    • Finally, adding the input day of the month.
  4. daysBetweenDates(date1, date2): This function orchestrates the whole process:

    • It calls calcDays for both input dates.
    • It takes the absolute difference between the two day counts to get the number of days between the two dates.

Time and Space Complexity

  • Time Complexity: O(Y), where Y is the number of years between the earliest date and the latest date among the input dates and 1971. This is dominated by the loop within calcDays. The number of months involved is relatively small compared to the number of years.

  • Space Complexity: O(1). The solution uses a fixed-size array (days in daysInMonth) to store the number of days in each month; its size is constant regardless of the input dates.

Code Examples

The provided code examples illustrate the solution in Python, Java, C++, Go, and TypeScript. The structure and logic are consistent across all languages, reflecting the algorithmic approach. They all follow the same function breakdown: isLeapYear, daysInMonth, calcDays, and daysBetweenDates. The only differences are language-specific syntax and library calls (e.g., abs for absolute value).

This detailed explanation clarifies the approach, complexity, and code implementation for solving the "Number of Days Between Two Dates" problem efficiently.