Isoweekday() Function Of Datetime.date Class In Python
isoweekday() is a function that returns an integer that tells the given date falls on. The integer it returns represents a day according to the table given below.
Syntax: datetime.isoweekday()
Return Value: an integer in range of [1,7]
Integer Returned |
Day of the week |
1 |
Monday |
2 |
Tuesday |
3 |
Wednesday |
4 |
Thursday |
5 |
Friday |
6 |
Saturday |
7 |
Sunday |
Example 1: This program grabs the date using the DateTime module and tells the day of the date.
Python3
import datetime
DaysList = [ "None" ,
"Monday" ,
"Tuesday" ,
"Wednesday" ,
"Thursday" ,
"Friday" ,
"Saturday" ,
"Sunday" ]
CurrentDate = datetime.date.today()
print ( "Current Date is :" , CurrentDate)
day = CurrentDate.isoweekday()
print ( "The date" , CurrentDate, "falls on" ,
DaysList[day])
|
Output:
Current Date is : 2021-08-18
The date 2021-08-18 falls on Wednesday
Example 2: In this example, we will take the user’s input for a date and will return the day it falls on.
Python3
import datetime
DaysList = [ "None" ,
"Monday" ,
"Tuesday" ,
"Wednesday" ,
"Thursday" ,
"Friday" ,
"Saturday" ,
"Sunday" ]
day = 18
month = 9
year = 2020
Date = datetime.date(year, month, day)
day = Date.isoweekday()
print ( "The given date" , Date, "falls on" ,
DaysList[day])
|
Output:
The given date 2020-09-18 falls on Friday
Example 3: In this example, we will take the user’s input for the date and the day that it falls on and return if that’s true or not
Python3
import datetime
DaysList = [ "None" ,
"Monday" ,
"Tuesday" ,
"Wednesday" ,
"Thursday" ,
"Friday" ,
"Saturday" ,
"Sunday" ]
day = 1
month = 1
year = 2021
day_fallen = "Friday"
Date = datetime.date(year, month, day)
day = Date.isoweekday()
if day_fallen.lower() = = DaysList[day].lower():
print ( "Yes, your given date" , Date,
"falls on your expected day i.e " ,
DaysList[day])
else :
print ( "No, your given date" , Date, "falls on" ,
DaysList[day],
"but not on" , day_fallen)
|
Output:
Yes, your given date 2021-01-01 falls on your expected day i.e Friday
Last Updated :
31 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...