Get date from ISO week number in Python

Erik Cederstrand picture Erik Cederstrand · May 4, 2011 · Viewed 28.3k times · Source

Possible Duplicate:
What’s the best way to find the inverse of datetime.isocalendar()?

I have an ISO 8601 year and week number, and I need to translate this to the date of the first day in that week (Monday). How can I do this?

datetime.strptime() takes both a %W and a %U directive, but neither adheres to the ISO 8601 weekday rules that datetime.isocalendar() use.

Update: Python 3.6 supports the %G, %V and %u directives also present in libc, allowing this one-liner:

>>> datetime.strptime('2011 22 1', '%G %V %u')
datetime.datetime(2011, 5, 30, 0, 0)

Update 2: Python 3.8 added the fromisocalendar() method, which is even more intuitive:

>>> datetime.fromisocalendar(2011, 22, 1)
datetime.datetime(2011, 5, 30, 0, 0)

Answer

gisle picture gisle · Oct 7, 2011

With the isoweek module you can do it with:

from isoweek import Week
d = Week(2011, 40).monday()