You are currently viewing Number of Days Between Two Dates | Python | Leetcode 1360

Number of Days Between Two Dates | Python | Leetcode 1360

  • Post author:
  • Post category:Leetcode

Question

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.

Solutions

1) Using datetime module

The datetime module supplies classes for manipulating dates and times. The strptime() method converts the given string to a datetime object. It takes two arguments, the string that has be converted to datetime and the format code.

from datetime import datetime
class Solution:
    def daysBetweenDates(self, date1: str, date2: str) -> int:
        x = datetime.strptime(date1, '%Y-%m-%d')
        y = datetime.strptime(date2, '%Y-%m-%d')
        return abs((y - x).days)

Time Complexity : O(1)

Space Complexity : O(1)


2) Without datetime module

Intuition

In this solution, we will have a starting point that is a year before 1971. Let’s choose 1970 as the starting point.

Now, we will count the number of days that have passed from 1970 to date1. Similarly, we will count the number of days that have passed from 1970 to date2. Finally we will return the absolute difference between two counts.

We will also keep in account if the year is a leap year or not. An year is a leap year if it divisible by 4. A century year (year ending with 00 and divisible by 100) is a leap year when it is divisible by 400. Every leap year adds one extra day (29 Feb) to total days. Keeping this is in mind let’s write the code.

Approach

We have variable start_year as our starting point. We will then split our input date, using the split method into year, month and day.

We now have to count the number of days from the start_year to the year before the given input. Thus, we will iterate using range(). Each year has 365 days. While iterating through the years, we will also check if the year y is leap or not. If it is a leap year we will increment the days by 1.

def count_days(date):
    start_year = "1970"
    year,month,day = date.split('-')
    
    days = 0 
    for y in range(int(start_year),int(year)):
        days += 365
        if y%100 == 0:
            if y%400 == 0:
                days += 1
        else:
            if y%4 == 0: 
                days += 1

After getting the number of days, from the previous years, we will count the number of days in the current year. Therefore, will iterate till the given month input. We will check the value for each month m and add number of days accordingly. If the month is February, we will check if the current year is leap or not. If it is leap year, we will increment the days by 1 since in leap year February has 29 days.

    for m in range(int(month)):
        if m in [1,3,5,7,8,10,12]:
            days += 31
        if m in [4,6,9,11]:
            days += 30
        if m == 2:
            days += 28
            if (int(year))%100 == 0:
                if (int(year))%400 == 0:
                    days += 1
            else:
                if (int(year))%4 == 0:
                    days += 1

Then we will add the rest of the days and return the days count to the function. Finally, we return the absolute difference between the two dates.  

   days += int(day)
            return days
        return(abs(count_days(date1)- count_days(date2)))

Whole code

class Solution:
    def daysBetweenDates(self, date1: str, date2: str) -> int:
        def count_days(date):
            start_year = "1970"
            year,month,day = date.split('-')
            days = 0 
            for y in range(int(start_year),int(year)):
                days += 365
                if y%100 == 0:
                    if y%400 == 0:
                        days += 1
                else:
                    if y%4 == 0:
                        days += 1
            for m in range(int(month)):
                if m in [1,3,5,7,8,10,12]:
                    days += 31
                if m in [4,6,9,11]:
                    days += 30
                if m == 2:
                    days += 28
                    if (int(year))%100 == 0:
                        if (int(year))%400 == 0:
                            days += 1
                    else:
                        if (int(year))%4 == 0:
                            days += 1
            days += int(day)
            return days
        return(abs(count_days(date1)- count_days(date2)))

Time Complexity : O(n)

Space Complexity : O(1)