[Tutor] datetime: inverse of datetime.date.isocalendar()

Alan Gauld alan.gauld at btinternet.com
Fri Jul 12 21:00:46 CEST 2013


On 12/07/13 17:52, elmar werling wrote:

> how to convert the tuble (iso_year, iso_week, iso_day) to a date object?
>
> I have tried the following scipt, but the results are buggy

Thanks for sending the code but can you define buggy?
What did you expect? What did you get?
Were there any error messages if so what (full text please)?


> import datetime
>
> date_i = datetime.date(2013, 07, 12)
> date_iso = date_i.isocalendar()
>
> year = str(date_iso[0])[2:]
> week = str(date_iso[1])
> day = str(date_iso[2])
>
> date_string = year + week + day

But guess is there is a better way of doing the lines above
but I don't know the datetime module well enough to say what...

> date_ii = datetime.datetime.strptime(date_string,'%y%W%w').date()
>
> print date_i, date_iso, date_string, date_ii

I'm not sure what you expect to see or what differs
from your expectation?


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list