Home >Backend Development >Python Tutorial >How to Get the Day of the Week from a Date in Python?
Retrieving the Day of the Week from a Date in Python
A common programming task involves determining the day of the week corresponding to a given date. This can be achieved in Python using the datetime.datetime.weekday() method.
Using weekday()
To obtain the day of the week, simply call the weekday() method on the datetime object:
import datetime today = datetime.datetime(2017, 10, 20) day_of_week = today.weekday()
Interpretation
The weekday() method returns an integer value representing the day of the week, with Monday as 0 and Sunday as 6. For example, Friday would be represented as 6.
Example Output
Using the date from the example provided:
>>> today = datetime.datetime(2017, 10, 20) >>> today.weekday() 6
Output: 6 (Friday)
The above is the detailed content of How to Get the Day of the Week from a Date in Python?. For more information, please follow other related articles on the PHP Chinese website!