.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 = 0…
Sunday = 4 (for that week only).
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()
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:
- the if condition was wrong
- days are represented inconsistently: