How to get Number of Days between Two Dates in Python?


Python - Get Number of Days between Two Dates

To get the number of days between two given dates in Python, follow these steps.

  • Import datetime library.
  • Take the two dates as date objects.
  • Find the difference between these two dates using Subtraction Operator. Subtraction Operator with the two dates as operands returns a timedelta object.
  • timedelta.days returns the number of days between the two given dates.

Program

In the following program, we take two dates: date_1 and date_2 as date objects, and find the number of days between them.

Python Program

from datetime import date

date_1 = date(2022, 1, 18)
date_2 = date(2022, 2, 14)
diff = date_2 - date_1
num_days = diff.days
print('Number of days :', num_days)

Output #1

Number of days : 27

Summary

In this Python Tutorial, we learned how to find the number of days between two given dates, using datetime library.