8

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

Ram Rachum
  • 84,019
  • 84
  • 236
  • 374

1 Answers1

4

EDIT Found question with solution: What's the best way to find the inverse of datetime.isocalendar()?

My solution had mistakes. On googling saw previous question which upon testing below worked. (See top answered question in above link for definition of iso_to_gregorian ). (Basically find iso year start date and then use timedelta to find current date from 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
Community
  • 1
  • 1
dr jimbob
  • 17,259
  • 7
  • 59
  • 81