How to get the number of days between two dates in python ?

Published: January 09, 2024

Tags: Python;

DMCA.com Protection Status

Introduction

When working with dates and times, it is common to need to calculate the difference between two dates. In this case, you may want to get the number of days between two given dates.

Using the datetime Module

One way to get the number of days between two dates in Python is by using the datetime module.

By utilizing the datetime module, you have the ability to create various objects such as a date object (based on year, month, and day) or a datetime object (based on year, month, day, hour, and minute). With these objects, you can easily calculate the difference in days. It is imperative to exercise caution when creating these objects, as the resulting output can either be an integer or a float, depending on the context.

Using date objects

This module provides a date object that represents a date and allows for various calculations to be done on it.
To start, you will need to import the datetime module:

import datetime

Next, create two date objects using the datetime.date() constructor. As an illustration, let's consider two date instances: one representing August 3rd, 2019 and the other representing August 9th, 2019:

start_date = datetime.date(2019, 8, 3)

end_date = datetime.date(2019, 8, 9)

Now that we have our two dates, we can use the timedelta object to calculate the difference between them. The timedelta object represents a duration of time and can be used to perform arithmetic on dates. We can create a timedelta representing 6 days (the difference between August 9th and August 3rd) and then use the days attribute to get the number of days:

delta = end_date - start_date

print(delta.days)

This will output

6

, indicating that there are 6 days between the two dates.

Using datetime objects

Let's now create two datetime objects and explore how to calculate the duration between them in terms of days.Please note that it is slightly distinct from the previous example. It is not just a date, but a datetime that includes the hour and minutes. For this, we can use the datetime module from python. Let's import it:

import datetime

Now, let's create our two dates as datetime objects:

start_date = datetime.datetime(2019, 8, 3, 15, 30)

end_date = datetime.datetime(2019, 8, 9, 20, 42)

These two dates represent August 3rd, 2019 at 15:30 PM and August 9th, 2019 at 20:42 PM.
To find the number of days between these two dates, we can use the end_date - start_date syntax. This will give us a datetime object that represents the duration between start_date and end_date. To get only the number of days, we can use the .days attribute on this datetime object. So, in our case, the code would be:

duration = (end_date - start_date).days

Which will give us a duration of 6 days.

Using datetime objects has the advantage of allowing you to obtain a fraction of a day from a timedelta. For example:

duration = (end_date - start_date).total_seconds() / 86400

Output

6.216666666666667

Using the numpy Module

Another way to get the number of days between two dates is by using the numpy module. This module provides a powerful datetime64 data type which can be used to represent dates with nanosecond precision.

Be cautious when defining your arrays, just like in the previous example. Pay attention to how you declare and use them.

datetime64[D]

from numpy import timedelta64

D1 = np.array(['2019-07-23', '2019-08-03', '2019-08-29'], dtype='datetime64[D]')

D2 = np.array(['2019-07-15', '2019-08-22', '2019-08-29'], dtype='datetime64[D]')

DELTA = D2 - D1

This will output

array([-8, 19,  0], dtype='timedelta64[D]')

Using datetime64

from numpy import timedelta64

D1 = np.array(['2019-07-23T13:30', '2019-08-03T13:30', '2019-08-29T03:30'], dtype='datetime64')

D2 = np.array(['2019-07-15T17:30', '2019-08-22T03:30', '2019-08-29T03:30'], dtype='datetime64')

DELTA = D2 - D1

Please note that in this case, the difference between the two dates is measured in minutes:

array([-11280,  26760,      0], dtype='timedelta64[m]')

If we wish to express it in terms of days, we can follow the following approach:

DELTA.astype('int') * 1.0 / (24 * 60)

This will output

array([-7.83333333, 18.58333333,  0.        ])

References