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?
from datetime import datetime # If today is Monday (0 = Mon, 1 = Tue, 2 = Wen ...) if datetime. today(). weekday() == 0: print("Yes, Today is Monday") else: print("Nope...") from datetime import datetime # If today is Monday (1 = Mon, 2 = Tue, 3 = Wen ...) if datetime.
Use the getDay() method to check if a date is a Monday, e.g. date. getDay() === 1 . The method returns a number between 0 and 6 for the day of the week, where Monday is 1 .
You can use equal to comparison operator = to check if one datetime object is has same value as other.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With