How do you get business days count between two dates in Python?

We will need two modules to access date() class and busday_count() method for getting business dates between two dates in python which are datetime and numpy. To begin with, let’s import both modules into the python file.

import datetime as dtModule
import numpy

The import keyword imports the module whereas as keyword refers datetime module to dt. In case you didn’t install numpy module then python would return an error: ModuleNotFoundError: No module named ‘numpy’

In this case you need to run the below command to install the numpy module before importing it.

pip3 install numpy

Next, let’s create two dates through date() class.

startDate = dt.date(2022, 3, 1)
endDate = dt.date(2022, 3, 31)

We created two dates in which first is 1st March 2022 and second is 31st March 2022. In the next step, we need to get the count of business days betweens the created dates.

To get the count of business days between two dates, we can use the busday_count() method that accepts two parameters in which the first is the start date and the second is the end date.

businessDays = numpy.busday_count(startDate, endDate)
print('count of business days:  ', businessDays)
# Output: 22

Conclusion

You need to use the date() class from the datetime module and busday_count() method from the numpy module to get business days count between two dates.