Python DateTime weekday() Method with Example - GeeksforGeeks

23 Aug, 2021
In this article, we will discuss the weekday() function in the DateTime module. weekday() function is used to get the week number based on given DateTime. It will return the number in the range of 0-6
It will take input as DateTime in the format of “(YYYY, MM, DD, HH, MM, SS)”, where,
YYYY stands for year
MM stands for Month
DD stands for Date
HH stands for Hour
MM stands for minute
SS stands for second
We first need to import the DateTime module and create a DateTime, now using weekday() function we will get the weekday for a particular DateTime.
Syntax:
datetime(YYYY,MM,DD, HH,MM,SS).weekday()
we can also extract date from DateTime by using the following syntax:
Syntax:
datetime(YYYY,MM,DD, HH,MM,SS).date()
Example: Python program to create DateTime and display DateTime and date
Python3
# importing datetime classfrom datetime import datetime  # create datetimex = datetime(2021, 8, 8, 12, 5, 6)  # displayprint("Datetime is :", x)    # get the dateprint("Date is :", x.date())
Output:
Datetime is : 2021-08-08 12:05:06
Date is : 2021-08-08
Example: Python program to get the weekdays for the given datetime(s)
Python3
# importing datetime classfrom datetime import datetime  # create datetimex = datetime(2021, 8, 8, 12, 5, 6)  # displayprint("Datetime is :", x)    # get the weekdayprint("weekday is :", x.weekday())    # create datetimex = datetime(2021, 9, 10, 12, 5, 6)  # displayprint("Datetime is :", x)    # get the weekdayprint("weekday is :", x.weekday())  # create datetimex = datetime(2020, 1, 8, 12, 5, 6)  # displayprint("Datetime is :", x)    # get the weekdayprint("weekday is :", x.weekday())
Output:
Datetime is : 2021-08-08 12:05:06
weekday is : 6
Datetime is : 2021-09-10 12:05:06
weekday is : 4
Datetime is : 2020-01-08 12:05:06
weekday is : 2
Example 3: Python program to get the name of weekday
Python3
# create a list of weekdaysfrom datetime import datetime  days = ["Monday", "Tuesday", "Wednesday",        "Thursday", "Friday", "Saturday", "Sunday"]  # importing datetime class  # create datetimex = datetime(2021, 8, 8, 12, 5, 6)  # displayprint("Datetime is :", x)    # get the weekday name using indexprint("weekday is :", days[x.weekday()])    # create datetimex = datetime(2021, 9, 10, 12, 5, 6)  # displayprint("Datetime is :", x)    # get the weekday name  using indexprint("weekday is :", days[x.weekday()])  # create datetime using indexx = datetime(2020, 1, 8, 12, 5, 6)  # displayprint("Datetime is :", x)    # get the weekday name using indexprint("weekday is :", days[x.weekday()])
Output:
Datetime is : 2021-08-08 12:05:06
weekday is : Sunday
Datetime is : 2021-09-10 12:05:06
weekday is : Friday
Datetime is : 2020-01-08 12:05:06
weekday is : Wednesday
Picked
Python-datetime
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()