What is date.isoweekday() in python?
This isoweekday() method returns an integer value that corresponds to the weekday. It returns 1 for Monday, 2 for Tuesday, and so on.
Note: The
isoweekday()method comes from thedateclass in Python.
Syntax
date.isoweekday()
Parameters
This method doesn’t take any argument value(s).
Return value
weekday: This method returns the day of a week as an integer value (1–7).
Weekday Number
Integer Value | Day of the week |
| Monday |
| Tuesday |
| Wednesday |
| Thursday |
| Friday |
| Saturday |
| Sunday |
Code example
The following code snippet elaborates on how we can use the isoweekday() method in a program:
# importing the date class from datetimefrom datetime import date# To show readbale results let's define# Dictionay with days labeled# From 1 to 7weekdays= { 1: "Monday",2: "Tuesday",3: "Wednesday",4: "Thursday",5: "Friday",6: "Saturday",7: "Sunday"}# Getting today's datetodays_date = date.today()print("Today's date:", todays_date)# Using the isoweekday() function# From date classdayNumber = todays_date.isoweekday()# Print results on consoleprint("Date:", todays_date, "falls on", weekdays[dayNumber])
Code explanation
- Line 2: We import the
dateclass from thedatetimemodule. - Line 6: We create a dictionary to show the weekdays.
- Line 15: We extract today’s date.
- Line 20: We get the weekday in the
dayNumbervariable, using theisoweekday()method. - Line 22: We print the results on the console with a mapped weekday.