Isoweekday() Method Of Datetime Class In Python
Last Updated :
14 Aug, 2021
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.
Python3
import datetime
weekdays = { 1 : "Monday" ,
2 : "Tuesday" ,
3 : "Wednesday" ,
4 : "Thursday" ,
5 : "Friday" ,
6 : "Saturday" ,
7 : "Sunday" }
todays_date = datetime.date.today()
print ( "Today's date is :" , todays_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
Python3
import datetime
weekdays = { 1 : "Monday" ,
2 : "Tuesday" ,
3 : "Wednesday" ,
4 : "Thursday" ,
5 : "Friday" ,
6 : "Saturday" ,
7 : "Sunday" }
Today = datetime.date.today()
current_year = Today.year
for i in range ( 2010 , current_year + 1 ):
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
Share your thoughts in the comments
Please Login to comment...