Python program to find number of days between two given dates

Given two dates, Write a Python program to find the total number of days between them.

Examples:

Input : dt1 = 13/12/2018, dt2 = 25/2/2019
Output : 74 days

Input : dt1 = 01/01/2004, dt2 = 01/01/2005
Output : 365 days

Naive Approach: One Naive Solution is to start from dt1 and keep counting days till dt2 is reached. This solution requires more than O(1) time.
A Better and Simple solution is to count total number of days before dt1 from i.e., total days from 00/00/0000 to dt1, then count total number of days before dt2. Finally return the difference between two counts.



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program two find number of 
# days between two given dates 
  
# A date has day 'd', month 'm' and year 'y'
class Date: 
    def __init__(self, d, m, y): 
        self.d = d            
        self.m = m   
        self.y = y   
  
# To store number of days in all months from 
# January to Dec. 
monthDays = [31, 28, 31, 30, 31, 30
                        31, 31, 30, 31, 30, 31 ]
  
# This function counts number of leap years 
# before the given date 
def countLeapYears(d):
      
    years = d.y 
      
    # Check if the current year needs to be considered
    # for the count of leap years or not 
    if (d.m <= 2) :
        years-= 1
          
    # An year is a leap year if it is a multiple of 4, 
    # multiple of 400 and not a multiple of 100. 
    return int(years / 4 - years / 100 + years / 400 )
   
  
# This function returns number of days between two  
# given dates 
def getDifference(dt1, dt2) :
   
    # COUNT TOTAL NUMBER OF DAYS BEFORE FIRST DATE 'dt1' 
  
    # initialize count using years and day 
    n1 = dt1.y * 365 + dt1.d 
  
    # Add days for months in given date 
    for i in range(0, dt1.m - 1) :
        n1 += monthDays[i] 
  
    # Since every leap year is of 366 days, 
    # Add a day for every leap year 
    n1 += countLeapYears(dt1) 
  
    # SIMILARLY, COUNT TOTAL NUMBER OF DAYS BEFORE 'dt2' 
  
    n2 = dt2.y * 365 + dt2.d 
    for i in range(0, dt2.m - 1) :
        n2 += monthDays[i] 
    n2 += countLeapYears(dt2) 
  
    # return difference between two counts 
    return (n2 - n1) 
   
  
# Driver program 
dt1 = Date(13, 12, 2018 )
dt2 = Date(25, 2, 2019 )
  
print(getDifference(dt1, dt2), "days")

chevron_right


Output:

74 days

 
Using Python datetime module:
Python comes with an inbuilt datetime module which helps us to solve various datetime related problems. In order to find the difference between two dates we simply input the two dates with date type and subtract them, which in turn provides us the number of days between the two dates.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find number of days
# between two given dates
from datetime import date
  
def numOfDays(date1, date2):
    return (date2-date1).days
      
# Driver program
date1 = date(2018, 12, 13)
date2 = date(2019, 2, 25)
print(numOfDays(date1, date2), "days")

chevron_right


Output:

74 days


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.