Find days between two dates in Python

[4290 views]




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:
  1. import datetime from datetime package
  2. 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
  3. 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)
                 




Health is Wealth? Check Best Medical Insurance for you:-



Comments










Search
Have Technical Doubts????


Hot Deals ends in













Technical Quizzes Specially For You:

Search Tags

    python code to find number of days within 2 dates

    how many days between two dates