Python datetime weekday() Method with Example

Python datetime.weekday() Method: In this tutorial, we will learn about the weekday() method of datetime class in Python with its usage, syntax, and examples. By Hritika Rajput Last updated : April 22, 2023

Python datetime.weekday() Method

The datetime.weekday() method returns the day of the week as an integer, where Monday is 0 and Sunday is 6. It is an instance method, i.e., it works on an instance of the class.

Module

The following module is required to use weekday() method:

import datetime

Class

The following class is required to use weekday() method:

from datetime import datetime

Syntax

The following is the syntax of weekday() method:

weekday()

Parameter(s)

The following are the parameter(s):

  • None

Return Value

The return type of this method is a number which tells us what is the day of the week on that day.

Example of datetime weekday() Method in Python

## importing datetime class
from datetime import datetime

## Creating an instance
x = datetime.today()
d = x.weekday()
print("Today's weekday number is:", d)

x = datetime(1996, 10,27, 21, 5, 5)
d1 = x.weekday()
xd = x.date()
print("Weekday number on the date", xd,"will be:",d1)
print()

## Since we know the number, 
## we can save them in a list and 
## print the day on that number
day =["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
print("Today's day is:",day[d])
print("Day on date", x," was:", day[d1])

Output

Today's weekday number is: 5
Weekday number on the date 1996-10-27 will be: 6

Today's day is: Saturday
Day on date 1996-10-27 21:05:05  was: Sunday



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.