+1 vote
in Programming Languages by (40.5k points)
How can I find the day of the week for any given date in Python?

E.g.

8/2/2022 : Tuesday

1 Answer

+3 votes
by (349k points)
selected by
 
Best answer

The strftime() function of the datetime module takes a parameter to format the output.

You can specify the parameter '%a' for the shorter version (e.g. Wed) of the day and '%A' for the full version (e.g. Wednesday).

Here is an example:

>>> from datetime import datetime
>>> d=datetime(1998,12,3)
>>> d.strftime("%a")
'Thu'
>>> d.strftime("%A")
'Thursday'
>>> d=datetime(2022,8,2)
>>> d.strftime("%A")
'Tuesday'
>>> d.strftime("%a")
'Tue'
>>>


...