Isoweekday() Method Of Datetime Class In Python - GeeksforGeeks

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
Example 1: Print the day of the current date.
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() functionweekdays = {1: "Monday",            2: "Tuesday",            3: "Wednesday",            4: "Thursday",            5: "Friday",            6: "Saturday",            7: "Sunday"} # Getting current date using today()# function of the datetime classtodays_date = datetime.date.today()print("Today's date is :", todays_date) # Using the isoweekday() function to# retrieve the day of the given dateday = 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
# 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() functionweekdays = {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 attributeToday = 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
anikakapoor
as5853535
Picked
Python-datetime
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
Selecting rows in pandas DataFrame based on conditions
How to drop one or multiple columns in Pandas Dataframe
Python | Get unique values from a list
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Defaultdict in Python
Python | os.path.join() method
Create a directory in Python
Bar Plot in Matplotlib