Hackerss.com is a community of amazing hackers

Hackerss is a community for developers, data scientitst, ethical hackers, hardware enthusiasts or any person that want to learn / share their knowledge of any aspect of digital technology.

Create account Log in

Posted on

Python Calculate number of days between two dates.

#import datetime module
import datetime

#create a function that calculates the number of days between two dates
def days_between_dates(date1, date2):
    Calculate the number of days between two dates.

    date1 : datetime.date
        Start date.
    date2 : datetime.date
        End date.

        The number of days between ``date1`` and ``date2``.


    #calculate the number of days between the two dates
    return abs(date2 - date1).days

result = days_between_dates(datetime.date(2016, 1, 1), datetime.date(2016, 8, 27))
print(result)  #63
Enter fullscreen mode Exit fullscreen mode

Top comments (0)