What is the datetime date.weekday() function in Python?
Overview
In the datetime module, the date.weekday() method is used to extract the day of the week.
Note: The
datetimemodule in Python deals with time and date manipulation.
Syntax
date.weekday()
Parameters
This method does not take any argument value(s).
Return value
date.weekday() returns an integer with a value from 0 to 6.
| Day | Return value |
|---|---|
| Monday | 0 |
| Tuesday | 1 |
| Wednesday | 2 |
| Thursday | 3 |
| Friday | 4 |
| Saturday | 5 |
| Sunday | 6 |
Code example
In the code snippet below, we want to extract today’s date. We can use the weekday() method to also extract the day of the week (Monday–Sunday).
from datetime import date# Extract today datetoday = date.today()if today.weekday() == 0:print("Today is: Monday")elif today.weekday() == 1:print("Today is: Tuesday")elif today.weekday() == 2:print("Today is: Wednesday")elif today.weekday() == 3:print("Today is: Thursday")elif today.weekday() == 4:print("Today is: Friday")elif today.weekday() == 5:print("Today is: Saturday")else:print("Today is: Sunday")