Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if today is Monday in Python [duplicate]

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?

like image 556
sgerbhctim Avatar asked Aug 24 '17 21:08

sgerbhctim


People also ask

How do I check if today is Monday in Python?

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.

How do you know if today is Monday?

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 .

How do I check if two dates are the same in Python?

You can use equal to comparison operator = to check if one datetime object is has same value as other.


2 Answers

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()
like image 79
ettanany Avatar answered Oct 21 '22 05:10

ettanany


import datetime as dt

dt.date.today().isoweekday() == 1  # 1 = Monday, 2 = Tues, etc.
like image 39
Alexander Avatar answered Oct 21 '22 04:10

Alexander