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
JavaScript
x
3
1
>>> today = datetime.datetime(2017, 10, 20)
2
>>> today.get_weekday() # what I look for
3
The output is maybe 6
(since it’s Friday)
Advertisement
Answer
Use weekday()
:
JavaScript
1
6
1
>>> import datetime
2
>>> datetime.datetime.today()
3
datetime.datetime(2012, 3, 23, 23, 24, 55, 173504)
4
>>> datetime.datetime.today().weekday()
5
4
6
From the documentation:
Return the day of the week as an integer, where Monday is 0 and Sunday is 6.