To find number of days from given two dates in Python. (Note: The difference of days is not between the two dates but including the start date)
Steps:
- import datetime from datetime package
- If the value of two date is in string format then first convert that to datetime object using strftime method
If your value is already in datetime format then no need to convert it to datetime object
- Then find the date value from both the dates and then find the difference
Program:
from datetime import datetime
date1 = '2020-07-01 22:03:00'
date2 = '2020-07-01 23:03:00'
#Convert string value to datetime format
date_format1 = datetime.strptime(date1, '%Y-%m-%d %H:%M:%S')
date_format2 = datetime.strptime(date2, '%Y-%m-%d %H:%M:%S')
print(date_format1, type(date_format1))
days = date_format2.day - date_format1.day
print(days+1)