Python: the opposite of `datetime.date.isocalendar ()`

The datetime module provides the date.isocalendar method, which, given the date, returns it in the format ([year], [week], [weekday]) . How do I go back? Given a tuple ([year], [week], [weekday]) , how can I get a date object?

+7
source share
1 answer

EDIT Found a solution question: What is the best way to find the inverse of datetime.isocalendar ()?

My decision had errors. On googling I saw a previous question, which after testing below worked. (See the top answer to the question in the link above for the definition of iso_to_gregorian ). (Basically find the start date of the year and then use timedelta to find the current date from the day and week number.

 for i in range(-10000,10000,1): x = datetime.datetime.today().date() + datetime.timedelta(i) x_iso = datetime.datetime.isocalendar(x) assert iso_to_gregorian(*x_iso) == x 
+4
source

All Articles