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

Using .weekday() to find the day of the week as an integer (Monday = 0Sunday = 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 = 0Sunday = 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()

Answer

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