Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to find out the following: given a date (datetime object), what is the corresponding day of the week?

For instance, Sunday is the first day, Monday: second day.. and so on

And then if the input is something like today's date.

Example

>>> today = datetime.datetime(2017, 10, 20)
>>> today.get_weekday()  # what I look for

The output is maybe 6 (since it's Friday)

like image 961
frazman Avatar asked Mar 23 '12 22:03

frazman


People also ask

How do you find the day of the week for any date Python?

Use the weekday() method The weekday() method returns the day of the week as an integer, where Monday is 0 and Sunday is 6. For example, the date(2022, 05, 02) is a Monday.


2 Answers

Use weekday():

>>> import datetime
>>> datetime.datetime.today()
datetime.datetime(2012, 3, 23, 23, 24, 55, 173504)
>>> datetime.datetime.today().weekday()
4

From the documentation:

Return the day of the week as an integer, where Monday is 0 and Sunday is 6.

like image 193
Simeon Visser Avatar answered Oct 09 '22 23:10

Simeon Visser


If you'd like to have the date in English:

from datetime import date
import calendar
my_date = date.today()
calendar.day_name[my_date.weekday()]  #'Wednesday'
like image 378
seddonym Avatar answered Oct 10 '22 01:10

seddonym