snippetpythonCritical
How do I get the day of week given a date?
Viewed 0 times
howthegetweekgivendateday
Problem
I want to find out the following:
given a date (
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
The output is maybe
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 forThe output is maybe
6 (since it's Friday)Solution
Use
From the documentation:
Return the day of the week as an integer, where Monday is 0 and Sunday is 6.
weekday():>>> import datetime
>>> datetime.datetime.today()
datetime.datetime(2012, 3, 23, 23, 24, 55, 173504)
>>> datetime.datetime.today().weekday()
4From the documentation:
Return the day of the week as an integer, where Monday is 0 and Sunday is 6.
Code Snippets
>>> import datetime
>>> datetime.datetime.today()
datetime.datetime(2012, 3, 23, 23, 24, 55, 173504)
>>> datetime.datetime.today().weekday()
4Context
Stack Overflow Q#9847213, score: 1425
Revisions (0)
No revisions yet.