W3docs

How do I get the day of week given a date?

You can use the datetime module in Python to get the day of the week for a given date.

You can use the datetime module in Python to get the day of the week for a given date. Here is an example code snippet that demonstrates how to use the datetime.weekday() function to get the day of the week for a specific date:

use the datetime.weekday function to get the day of the week for a specific date

from datetime import datetime

date_string = "2022-11-20"
date_object = datetime.strptime(date_string, "%Y-%m-%d")

day_of_week = date_object.weekday()
print(day_of_week)

In this example, day_of_week will contain the day of the week as an integer, where Monday is 0, Tuesday is 1, and so on.

Alternatively, you could use the date.weekday() function

use the date.weekday function to get the day of the week for a specific date

from datetime import date

date_string = "2022-11-20"
date_object = date.fromisoformat(date_string)

day_of_week = date_object.weekday()
print(day_of_week)

Both of the above snippet will give you the same output.