How to get the date from the week number with Python?

Sometimes, we want to get the date from the week number with Python.

In this article, we’ll look at how to get the date from the week number with Python.

How to get the date from the week number with Python?

To get the date from the week number with Python, we can use the datetime.datetime.strptime method.

For instance, we write:

import datetime

d = "2020-W26"
r = datetime.datetime.strptime(d + '-1', "%Y-W%W-%w")
print(r)

We call datetime.datetime.strptime with the d date string.

And then we convert the d string to a date by concatenating it with '-1' and use "%Y-W%W-%w" as the format string.

Finally, we assign the returned date to r.

Therefore, r is '2020-06-29 00:00:00'.

Conclusion

To get the date from the week number with Python, we can use the datetime.datetime.strptime method.