Categories
Python Answers

How to get the week number in Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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