Isoweekday() Function Of Datetime.date Class In Python
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()
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
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 moduleimport datetime # Creating an list which will# be used to retrieve the day of the# week using the return value of the# isoweekday() functionDaysList = ["None", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] # Using the function today()# to get today's dateCurrentDate = datetime.date.today()print("Current Date is :", CurrentDate) # Using the isoweekday() function to# retrieve the day of the given dateday = 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 moduleimport 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()# functionDaysList = ["None", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]# Getting the user's inputday = 18month = 9year = 2020# Creating the datetime object for the user's# input by using the date() function of datetime# classDate = datetime.date(year, month, day)# Using the isoweekday() function# to retrieve the day of the given dateday = 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 moduleimport 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()# functionDaysList = ["None", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]# Getting the user's inputday = 1month = 1year = 2021day_fallen = "Friday"# Creating the datetime object for the user's# input by using the date() function of datetime# classDate = datetime.date(year, month, day)# Using the isoweekday() function# to retrieve the day of the given dateday = Date.isoweekday()# Checking if the day is matching the user's# dayif 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


