Domain 1EasyCORE

Number of Days Between Two Dates

The problem requires counting the number of days between two given dates in the format `YYYY-MM-DD`. The inputs are two strings representing valid dates between the years 1971 and 2100. The output should be an integer representing the absolute difference in days between these two dates.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#**FAANG asked:** 1

1360. Number of Days Between Two Dates

Model: qwen2.5-coder:14b
Topics:
FAANG asked: 1
Acceptance rate: 46.6%


Problem Breakdown

The problem requires counting the number of days between two given dates in the format YYYY-MM-DD. The inputs are two strings representing valid dates between the years 1971 and 2100. The output should be an integer representing the absolute difference in days between these two dates.

Inputs:

  • Two strings, date1 and date2, each formatted as YYYY-MM-DD.

Outputs:

  • An integer representing the number of days between the two dates.

Implicit Constraints or Edge Cases:

  • The dates are always valid.
  • The order of the input dates does not matter (i.e., the difference should be positive regardless of which date comes first).
  • Leap years need to be considered correctly (e.g., February 29th exists in leap years).

Intuition

A brute-force approach would involve calculating the number of days from a fixed reference point (like January 1, 1970) for each date and then finding the difference. However, this approach is inefficient and complex to implement correctly due to handling leap years and varying month lengths.

The key insight is to use Python’s built-in datetime module, which provides a straightforward way to handle dates and calculate differences between them. This approach leverages optimized libraries for date calculations, ensuring accuracy and efficiency.

Approach

  1. Parse the Input Dates: Convert the input strings into datetime.date objects using the strptime method.
  2. Calculate the Difference: Use the subtraction operator on datetime.date objects to get a timedelta object representing the difference between the two dates.
  3. Extract the Number of Days: Access the days attribute of the timedelta object to get the absolute number of days.

Complexity Analysis

  • Time Complexity: O(1) - The operations performed are constant time, as they involve simple date parsing and arithmetic.
  • Space Complexity: O(1) - No additional data structures are used that grow with input size.

Solution

from datetime import datetime

class Solution:
    def daysBetweenDates(self, date1: str, date2: str) -> int:
        # Parse the dates into datetime.date objects
        d1 = datetime.strptime(date1, "%Y-%m-%d").date()
        d2 = datetime.strptime(date2, "%Y-%m-%d").date()
        
        # Calculate the difference in days
        delta = abs((d2 - d1).days)
        
        return delta

Edge Cases Handled

  • Leap Years: The datetime module correctly handles leap years, ensuring that February 29th is accounted for.
  • Same Date: If both dates are the same, the difference will be zero days.
  • Order of Dates: The subtraction operation between datetime.date objects returns a positive timedelta regardless of the order of inputs.