views:

67

answers:

1

What is the proper way to convert a timedelta object into a datetime object? I immediately think of something like datetime(0)+deltaObj but that's not very nice... isn't there a toDateTime() function or something of the sort?

+5  A: 

It doesn't make sense to convert a timedelta into a datetime, but it does make sense to pick an initial or starting datetime and add or subtract a timedelta from that.

>>> import datetime
>>> today = datetime.datetime.today()
>>> today
datetime.datetime(2010, 3, 9, 18, 25, 19, 474362)
>>> today + datetime.timedelta(days=1)
datetime.datetime(2010, 3, 10, 18, 25, 19, 474362)
Roger Pate
+1: Epochal dates. March 8th is my daughter's birthday, that's a better epochal date than March 9th. Unix folks like Jan 1, 1970. Don't know why, that's not my daughter's birthday.
S.Lott