Isoweekday() is a method of the DateTime class that tells the day of the given date. It returns an integer that corresponds to a particular day.
Syntax: datetime.isoweekday()
Parameters: None
Return Value: It returns an integer which corresponds to a day as per the table
Integer Returned | Day of the week |
---|---|
1 | Monday |
2 | Tuesday |
3 | Wednesday |
4 | Thursday |
5 | Friday |
6 | Saturday |
7 | Sunday |
Example 1: Print the day of the current date.
# 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 weekdays = { 1 : "Monday" ,
2 : "Tuesday" ,
3 : "Wednesday" ,
4 : "Thursday" ,
5 : "Friday" ,
6 : "Saturday" ,
7 : "Sunday" }
# Getting current date using today() # function of the datetime class todays_date = datetime.date.today()
print ( "Today's date is :" , todays_date)
# Using the isoweekday() function to # retrieve the day of the given date day = todays_date.isoweekday()
print ( "The date" , todays_date, "falls on" ,
weekdays[day])
|
Output:
Today's date is : 2021-07-27 The date 2021-07-27 falls on Tuesday
Example 2: Get the day of the week for today’s date from 2010 to the current year
# 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 weekdays = { 1 : "Monday" ,
2 : "Tuesday" ,
3 : "Wednesday" ,
4 : "Thursday" ,
5 : "Friday" ,
6 : "Saturday" ,
7 : "Sunday" }
# Getting current year using today() function # of the datetime class and the year attribute Today = datetime.date.today()
current_year = Today.year
for i in range ( 2010 , current_year + 1 ):
# Printing the day of the year
# by first creating an datetime object
# for the starting day of the year and
# then we use isoweekday
# to get the value and we use the
# weekdays to retrieve the day of the year
print ( "The {}/{} in the year {} has fallen on {}" .\
format (Today.month, Today.day, i,
weekdays[datetime.date(i, Today.month,
Today.day).isoweekday()]))
|
Output:
The 7/28 in the year 2010 has fallen on Wednesday
The 7/28 in the year 2011 has fallen on Thursday
The 7/28 in the year 2012 has fallen on Saturday
The 7/28 in the year 2013 has fallen on Sunday
The 7/28 in the year 2014 has fallen on Monday
The 7/28 in the year 2015 has fallen on Tuesday
The 7/28 in the year 2016 has fallen on Thursday
The 7/28 in the year 2017 has fallen on Friday
The 7/28 in the year 2018 has fallen on Saturday
The 7/28 in the year 2019 has fallen on Sunday
The 7/28 in the year 2020 has fallen on Tuesday
The 7/28 in the year 2021 has fallen on Wednesday