• Post author:
  • Post category:Python
  • Reading time:1 mins read
Get Working Days Using Python

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

Advertisement

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.

Advertisement
Related:  Split a JSON File into Separate Files in Python

Vinish Kapoor

An Oracle Apex Consultant, Oracle ACE, and founder of foxinfotech.in and orclqa.com a question and answer forum for developers.