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

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

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.

If youd like to have the date in English:

from datetime import date
import calendar
my_date = date.today()
calendar.day_name[my_date.weekday()]  #Wednesday

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

If youd like to have the date in English:

>>> from datetime import datetime
>>> datetime.today().strftime(%A)
Wednesday

Read more:
https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior

Leave a Reply

Your email address will not be published. Required fields are marked *