Press "Enter" to skip to content

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

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.

Have you found the answer to your question? If not, you can discuss it with me in the comments section below or join my Q&A community OrclQA.com for developers and ask your question. It is FREE.

Vinish Kapoor

Follow

Hi, I am a full stack developer and writing about development. I document everything I learn and help thousands of people. foxinfotech.in is created, written, and maintained by me; it is built on WordPress, and hosted by Bluehost. Connect with me on Facebook, Twitter, GitHub, and get notifications for new posts.

guest
0 Comments
Inline Feedbacks
View all comments