Home >Backend Development >Python Tutorial >How to Get the Day of the Week from a Date in Python?

How to Get the Day of the Week from a Date in Python?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-01 22:46:12788browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn