isoweekday() is a function that returns an integer that tells the given date falls on. The integer it returns represents a day according to the table given below.
Syntax: datetime.isoweekday()
Return Value: an integer in range of [1,7]
Integer Returned | Day of the week |
---|---|
1 | Monday |
2 | Tuesday |
3 | Wednesday |
4 | Thursday |
5 | Friday |
6 | Saturday |
7 | Sunday |
Example 1: This program grabs the date using the DateTime module and tells the day of the date.
Python3
# importing the datetime module import datetime # Creating an list which will # be used to retrieve the day of the # week using the return value of the # isoweekday() function DaysList = [ "None" , "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday" , "Sunday" ] # Using the function today() # to get today's date CurrentDate = datetime.date.today() print ( "Current Date is :" , CurrentDate) # Using the isoweekday() function to # retrieve the day of the given date day = CurrentDate.isoweekday() print ( "The date" , CurrentDate, "falls on" , DaysList[day]) |
Output:
Current Date is : 2021-08-18 The date 2021-08-18 falls on Wednesday
Example 2: In this example, we will take the user’s input for a date and will return the day it falls on.
Python3
# importing the datetime module import datetime # Creating an dictionary with the return # value as keys and the day as the value # This is used to retrieve the day of the week # using the return value of the isoweekday() # function DaysList = [ "None" , "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday" , "Sunday" ] # Getting the user's input day = 18 month = 9 year = 2020 # Creating the datetime object for the user's # input by using the date() function of datetime # class Date = datetime.date(year, month, day) # Using the isoweekday() function # to retrieve the day of the given date day = Date.isoweekday() print ( "The given date" , Date, "falls on" , DaysList[day]) |
Output:
The given date 2020-09-18 falls on Friday
Example 3: In this example, we will take the user’s input for the date and the day that it falls on and return if that’s true or not
Python3
# importing the datetime module import datetime # Creating an dictionary with the return # value as keys and the day as the value # This is used to retrieve the day of the week # using the return value of the isoweekday() # function DaysList = [ "None" , "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday" , "Sunday" ] # Getting the user's input day = 1 month = 1 year = 2021 day_fallen = "Friday" # Creating the datetime object for the user's # input by using the date() function of datetime # class Date = datetime.date(year, month, day) # Using the isoweekday() function # to retrieve the day of the given date day = Date.isoweekday() # Checking if the day is matching the user's # day if day_fallen.lower() = = DaysList[day].lower(): print ( "Yes, your given date" , Date, "falls on your expected day i.e " , DaysList[day]) else : print ( "No, your given date" , Date, "falls on" , DaysList[day], "but not on" , day_fallen) |
Output:
Yes, your given date 2021-01-01 falls on your expected day i.e Friday