Sometimes, we want to get the day of week given a date with Python.
In this article, we’ll look at how to get the day of week given a date with Python.
How to get the day of week given a date with Python?
To get the day of week given a date with Python, we can use the weekday
method.
For instance, we write
import datetime
d = datetime.datetime.today().weekday()
to get today’s datetime with datetime.datetime.today()
.
And then we call weekday
on the datetime to get the day of the week of today as the number.
d
should be between 0 and 6 where 0 is Monday and 6 is Sunday.
Conclusion
To get the day of week given a date with Python, we can use the weekday
method.