Brian Pemberton
Brian Pemberton

Reputation: 23

How to print dates of weekdays only (not weekends) for a range of days by date, for a range of months?

I need to figure out in python how to (in pesudo)

x = month-begin
y = month-end

for range in (x through y) 
  print(dates of weekdays - Monday trough Friday)

Upvotes: 0

Views: 484

Answers (2)

user1763510
user1763510

Reputation: 1280

Use the datetime module:

import datetime
current=datetime.datetime(2018,8,14,14,00)  
end=datetime.datetime(2018,12,1,14,00) 
while current<end:
    current+=datetime.timedelta(days=1)
    if current.weekday()<5:
        print(current.strftime("%-m/%-d"))

Upvotes: 1

J. Blackadar
J. Blackadar

Reputation: 1961

Use date.weekday(). Following your pseudo-code example:

for day in range (x through y)
    if day.weekday() in range(0,5):
        print(day)

Upvotes: 2

Related Questions