Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python DateTime weekday() Method with Example

  • Last Updated : 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

RepresentationMeaning
0Monday
1Tuesday
2Wednesday
3Thursday
4Friday
5Saturday
6Sunday

It will take input as DateTime in the format of “(YYYY, MM, DD, HH, MM, SS)”, where,

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

  • 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 class
from datetime import datetime
  
# create datetime
x = datetime(2021, 8, 8, 12, 5, 6)
  
# display
print("Datetime is :", x)
  
  
# get the date
print("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 class
from datetime import datetime
  
# create datetime
x = datetime(2021, 8, 8, 12, 5, 6)
  
# display
print("Datetime is :", x)
  
  
# get the weekday
print("weekday is :", x.weekday())
  
  
# create datetime
x = datetime(2021, 9, 10, 12, 5, 6)
  
# display
print("Datetime is :", x)
  
  
# get the weekday
print("weekday is :", x.weekday())
  
# create datetime
x = datetime(2020, 1, 8, 12, 5, 6)
  
# display
print("Datetime is :", x)
  
  
# get the weekday
print("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 weekdays
from datetime import datetime
  
days = ["Monday", "Tuesday", "Wednesday",
        "Thursday", "Friday", "Saturday", "Sunday"]
  
# importing datetime class
  
# create datetime
x = datetime(2021, 8, 8, 12, 5, 6)
  
# display
print("Datetime is :", x)
  
  
# get the weekday name using index
print("weekday is :", days[x.weekday()])
  
  
# create datetime
x = datetime(2021, 9, 10, 12, 5, 6)
  
# display
print("Datetime is :", x)
  
  
# get the weekday name  using index
print("weekday is :", days[x.weekday()])
  
# create datetime using index
x = datetime(2020, 1, 8, 12, 5, 6)
  
# display
print("Datetime is :", x)
  
  
# get the weekday name using index
print("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




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!