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 the date class 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

1

Monday

2

Tuesday

3

Wednesday

4

Thursday

5

Friday

6

Saturday

7

Sunday

Code example

The following code snippet elaborates on how we can use the isoweekday() method in a program:

# importing the date class from datetime
from datetime import date
# To show readbale results let's define
# Dictionay with days labeled
# From 1 to 7
weekdays= { 1: "Monday",
2: "Tuesday",
3: "Wednesday",
4: "Thursday",
5: "Friday",
6: "Saturday",
7: "Sunday"}
# Getting today's date
todays_date = date.today()
print("Today's date:", todays_date)
# Using the isoweekday() function
# From date class
dayNumber = todays_date.isoweekday()
# Print results on console
print("Date:", todays_date, "falls on", weekdays[dayNumber])

Code explanation

  • Line 2: We import the date class from the datetime module.
  • 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 dayNumber variable, using the isoweekday() method.
  • Line 22: We print the results on the console with a mapped weekday.

Free Resources