Find the number of days between 2 dates
We can find the number of days and seconds between two date objects using just one line.
Python: 3.0    Anurag Rana    13 Jul 2021   0
Login to upvote
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from datetime import date

# create date objects
first_date= date(2021,1,22)
second_date = date(2021,2,5)

# find the difference
difference = second_date - first_date

# difference is the timedelta object
print(type(difference))
# <type 'datetime.timedelta'>

print(difference.days)
# 14
print(difference.total_seconds())
# 1209600.0
Copy | Download as: .py file   PNG
Share on: FB   Twitter   LinkedIn   WA
©2021 - code108labs   Contributors   Hosted on DigitalOcean   Partner website: pythoncircle.com