Get Working Days Using Python

Get Dates of Working Days Between Two Dates in Python (Exclude Weekends)

  • Python
  • 1 min read

Here I am giving an example to show you how to get dates of working days between two dates in Python by excluding weekends.

Python Workdays Example

In the below Python program, it will take three parameters, 1st is the start date, 2nd is the end date, and the third is default specified the day numbers 6 and 7, for Saturday and Sundays.

import datetime

def workdays(d, end, excluded=(6, 7)):
    days = []
    while d.date() <= end.date():
        if d.isoweekday() not in excluded:
            days.append(d)
        d += datetime.timedelta(days=1)
    return days

print(workdays(datetime.datetime(2019, 1, 21),
               datetime.datetime(2019, 1, 30)))

Output

[datetime.datetime(2019, 1, 21, 0, 0), datetime.datetime(2019, 1, 22, 0, 0), datetime.datetime(2019, 1, 23, 0, 0), datetime.datetime(2019, 1, 24, 0, 0), datetime.datetime(2019, 1, 25, 0, 0), datetime.datetime(2019, 1, 28, 0, 0), datetime.datetime(2019, 1, 29, 0, 0), datetime.datetime(2019, 1, 30, 0, 0)]

You can see in the above result that the weekend dates 26th (Saturday) and 27th (Sunday) of January 2019 are not present.