Number of Days Between Two Dates — LeetCode 1360 Python Solution

EasyMathString
Problem
#1360
Reading time
6 min

The problem

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

Input
date1 = "2019-06-29", date2 = "2019-06-30"
Output
1

Python solution

Python
class Solution:
    def daysBetweenDates(self, date1: str, date2: str) -> int:
        def isLeapYear(year: int) -> bool:
            return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)

        def daysInMonth(year: int, month: int) -> int:
            days = [
                31,
                28 + int(isLeapYear(year)),
                31,
                30,
                31,
                30,
                31,
                31,
                30,
                31,
                30,
                31,
            ]
            return days[month - 1]

        def calcDays(date: str) -> int:
            year, month, day = map(int, date.split("-"))
            days = 0
            for y in range(1971, year):
                days += 365 + int(isLeapYear(y))
            for m in range(1, month):
                days += daysInMonth(year, m)
            days += day
            return days

        return abs(calcDays(date1) - calcDays(date2))

Complexity

MeasureComplexity
TimeO(y + m), where y represents the number of years from the given date to `1971-01-01`, and m represents the number of months of the given date
SpaceO(1) auxiliary

Pattern: Math and Number Theory

Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 1360. Number of Days Between Two Dates is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math.

The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1360. Number of Days Between Two Dates?
LeetCode 1360. Number of Days Between Two Dates is rated Easy on LeetCode.
What is the time complexity of LeetCode 1360. Number of Days Between Two Dates?
The Python solution on this page runs in O(y + m), where y represents the number of years from the given date to `1971-01-01`, and m represents the number of months of the given date.
What is the space complexity of LeetCode 1360. Number of Days Between Two Dates?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 1360. Number of Days Between Two Dates cover?
LeetCode 1360. Number of Days Between Two Dates is tagged Math and String on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview