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?
asked Aug 24, 2017 at 21:01
3
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[]
answered Aug 24, 2017 at 21:06
ettananyettanany
18k6 gold badges42 silver badges60 bronze badges
1
import datetime as dt
dt.date.today[].isoweekday[] == 1 # 1 = Monday, 2 = Tues, etc.
answered Aug 24, 2017 at 21:04
AlexanderAlexander
99.3k28 gold badges187 silver badges185 bronze badges
3
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
answered Aug 24, 2017 at 21:26
lee-pai-longlee-pai-long
1,96715 silver badges16 bronze badges
corresponding to the day of the week. Here is the mapping of integer values to the days of the week.
# import Python's datetime module
import datetime
# weekdays as a tuple
weekDays = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
# Find out what day of the week is this year's Christmas
thisXMas = datetime.date[2017,12,25]
thisXMasDay = thisXMas.weekday[]
thisXMasDayAsString = weekDays[thisXMasDay]
print["This year's Christmas is on a {}".format[thisXMasDayAsString]]
# Find out what day of the week next new year is
nextNewYear = datetime.date[2018,1,1]
nextNewYearDay = nextNewYear.weekday[]
nextNewYearDayAsString = weekDays[nextNewYearDay]
print["Next new year is on a {}".format[nextNewYearDayAsString]]
Published On: 23/09/2022 | Category: Python
Hi Guys,
In this tutorial, you will learn python check date is monday or not. you can understand a concept of how to check if today is monday in python. I would like to show you how to check if today is monday in python. I’m going to show you about python today is monday. Let's see bellow example check today is monday in python.
In this example, we will use DateTime library to check whether today is Monday or not. I will give you two simple examples one using weekday[] and another using isoweekday[]. so let's see both examples:
You can use these examples with python3 [Python 3] version.
So let's see bellow example:
Example 1: Python Check Today's Date is Weekend or Weekday
main.py
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..."]
Output:
Yes, Today is Monday
Example 2: Python Check String Date is Weekend or Weekday
main.py
from datetime import datetime # If today is Monday [1 = Mon, 2 = Tue, 3 = Wen ...] if datetime.today[].isoweekday[] == 1: print["Yes, Today is Monday"] else: print["Nope..."]
Output:
Yes, Today is Monday
It will help you....
Happy Pythonic Coding!