Sometimes, we want to get the week number in Python.
In this article, we’ll look at how to get the week number in Python.
How to get the week number in Python?
To get the week number in Python, we can use the datetime’s isocalendar
method.
For instance, we write
import datetime
w = datetime.date(2021, 6, 16).isocalendar().week
to create a date with datetime.date
.
Then we call isocalendar
to return a named tuple with the week
property.
And we use that to get the week number.
Conclusion
To get the week number in Python, we can use the datetime’s isocalendar
method.