How would I write a statement that says:
If today is Monday, then run this function.
My thoughts are:
if datetime.now().day == Monday:
run_report()
But I know this is not the right way to do it. How would I properly do this?
How would I write a statement that says:
If today is Monday, then run this function.
My thoughts are:
if datetime.now().day == Monday:
run_report()
But I know this is not the right way to do it. How would I properly do this?
You can use date.weekday() like this:
from datetime import date
# If today is Monday (aka 0 of 6), then run the report
if date.today().weekday() == 0:
run_report()
import datetime as dt
dt.date.today().isoweekday() == 1 # 1 = Monday, 2 = Tues, etc.
Both datetime.date and datetime.datetime
objects have a today method that return respectively a Date and a Datetime object.
Which both have a weekday and isoweekday methods.
weekday count from Monday = 0, while isoweekday count from Monday = 1:
from datetime import date, datetime
if date.today().weekday() == 0:
# it is Monday
if datetime.today().isoweekday() == 1:
# it is Monday
See documentation