VOOZH about

URL: https://www.geeksforgeeks.org/python/python-program-to-find-number-of-days-between-two-given-dates/

⇱ Python program to find number of days between two given dates - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python program to find number of days between two given dates

Last Updated : 11 Jul, 2025

Finding the number of days between two dates means calculating how much time has passed from one date to another. This is helpful in real-life situations like tracking deadlines, calculating age, planning events or measuring durations in billing and scheduling.

Example:

Input: dt1 = 01/01/2004, dt2 = 01/01/2005
Output: 366 days

Python makes this easy through various methods. Let's explore them one by one.

Using datetime module

datetime module allows a program to easily calculate the number of days between two dates by subtracting one date object from another. It is a simple, built-in and reliable approach for handling date differences.

Example:

This code calculates and prints the number of days between two given dates using datetime module.


Output
792 days

Explanation:

  • d1 and d2 are created as date objects representing May 10, 2023 and July 10, 2025.
  • dif = d2 - d1 subtracts two dates resulting in a timedelta object.
  • dif.days returns the number of days between the two dates.

Using time module

time module can calculate the number of days between two dates by converting them into timestamps and finding the difference in seconds then dividing by 86,400 (seconds in a day).

Example: Here, time module is used to find the number of days between two dates by converting them into timestamps.


Output
792 days

Explanation:

  • date_format tells Python how to interpret the date strings.
  • time.strptime() converts the date string into a time structure (tuple format).
  • time.mktime() converts time structure to a Unix timestamp (in seconds).

Using dateutil.relativedelta

dateutil.relativedelta module allows programs to find difference between two dates in terms of years, months and days. It provides a more detailed and readable breakdown of the time gap.

Example: In this program dateutil.relativedelta is used to find the difference between two dates:


Output
2 years, 2 months, 0 days

Explanation: relativedelta(d2, d1) returns the difference as a relativedelta object, storing the gap in years, months and days.

Using reduce

reduce() applies a function to elements in a sequence. It can be used to find the number of days between two dates by subtracting them.

Example: This program uses reduce() to subtract two dates and find the number of days between them in a functional style.


Output
792 days

Explanation:

  • A list dates is created with two date objects: 2023-05-10 and 2025-07-10.
  • reduce(lambda x, y: (y - x).days, dates) subtracts first date from second and returns difference in days.

Related Articles:

Comment
Article Tags: