Open In App

Isoweekday() Function Of Datetime.date Class In Python

Last Updated : 31 Aug, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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 ReturnedDay of the week
1Monday
2Tuesday
3Wednesday
4Thursday
5Friday
6Saturday
7Sunday

Example 1: This program grabs the date using the DateTime module and tells the day of the date.

Python3
# importing the datetime module
import datetime
 
# Creating an list which will
# be  used to retrieve the day of the
# week using the return value of the
# isoweekday() function
DaysList  = ["None",
              "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday",
            "Sunday"]
 
# Using the function today()
# to get today's date
CurrentDate = datetime.date.today()
print("Current Date is :", CurrentDate)
 
# Using the isoweekday() function to
# retrieve the day of the given date
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
# 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
DaysList = ["None",
            "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday",
            "Sunday"]

# Getting the  user's input
day = 18
month = 9
year = 2020

# Creating the datetime object for the user's
# input by using the date() function of datetime 
# class
Date = datetime.date(year, month, day)

# Using the isoweekday() function
# to retrieve the day of the given date
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
# 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
DaysList = ["None",
            "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday",
            "Sunday"]

# Getting the  user's input
day = 1
month = 1
year = 2021
day_fallen = "Friday"

# Creating the datetime object for the user's
# input by using the date() function of datetime
# class
Date = datetime.date(year, month, day)

# Using the isoweekday() function
# to retrieve the day of the given date
day = Date.isoweekday()

# Checking if the day is matching the user's
# day
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


Next Article
Article Tags :
Practice Tags :

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy