How to Get Day of Week as Integer with First of Month Changing Values?

Viewed 328

Using .weekday() to find the day of the week as an integer (Monday = 0 ... Sunday = 6) for everyday from today until next year (+365 days from today). Problem now is that if the 1st of the month starts mid week then I need to return the day of the week with the 1st day of the month now being = 0.

Ex. If the month starts Wednesday then Wednesday = 0... Sunday = 4 (for that week only).

Annotated Picture of Month Explaining What I Want to Do

Originally had the below code but wrong as the first statement will run 7 days regardless.

import datetime
from datetime import date

for day in range (1,365):
    departure_date = date.today() + datetime.timedelta(days=day)

    if departure_date.weekday() < 7:
        day_of_week = departure_date.day
    else:
        day_of_week = departure_date.weekday()
3 Answers

The following seems to do the job properly:

import datetime as dt

def custom_weekday(date):
    if date.weekday() > (date.day-1):
        return date.day - 1
    else:
        return date.weekday()
        
for day in range (1,366):
    departure_date = dt.date.today() + dt.timedelta(days=day)
    day_of_week = custom_weekday(date=departure_date)
    print(departure_date, day_of_week, departure_date.weekday())

Your code had two small bugs:

  1. the if condition was wrong
  2. days are represented inconsistently: date.weekday() is 0-based, date.day is 1-based

For any date D.M.Y, get the weekday W of 1.M.Y.

Then you need to adjust weekday value only for the first 7-W days of that month. To adjust, simply subtract the value W.

Example for September 2021: the first date of month (1.9.2021) is a Wednesday, so W is 2. You need to adjust weekdays for dates 1.9.2021 to 5.9.2021 (because 7-2 is 5) in that month by minus 2.

For every date, get the first week of that month. Then, check if the date is within that first week. If it is, use the .day - 1 value (since you are 0-based). Otherwise, use the .weekday().

from datetime import date, datetime, timedelta

for day in range (-5, 40):
    departure_date = date.today() + timedelta(days=day)
    
    first_week = date(departure_date.year, departure_date.month, 1).isocalendar()[1]

    if first_week == departure_date.isocalendar()[1]:
        day_of_week = departure_date.day - 1
    else:
        day_of_week = departure_date.weekday()

    print(departure_date, day_of_week)
2021-08-27 4
2021-08-28 5
2021-08-29 6
2021-08-30 0
2021-08-31 1
2021-09-01 0
2021-09-02 1
2021-09-03 2
2021-09-04 3
2021-09-05 4
2021-09-06 0
2021-09-07 1
2021-09-08 2
2021-09-09 3
2021-09-10 4
2021-09-11 5
2021-09-12 6
2021-09-13 0
2021-09-14 1
2021-09-15 2
2021-09-16 3
2021-09-17 4
2021-09-18 5
2021-09-19 6
2021-09-20 0
2021-09-21 1
2021-09-22 2
2021-09-23 3
2021-09-24 4
2021-09-25 5
2021-09-26 6
2021-09-27 0
2021-09-28 1
2021-09-29 2
2021-09-30 3
2021-10-01 0
2021-10-02 1
2021-10-03 2
2021-10-04 0
2021-10-05 1
2021-10-06 2
2021-10-07 3
2021-10-08 4
2021-10-09 5
2021-10-10 6
Related