[Tutor] datetime: What happended yesterday? :-)

Kent Johnson kent37 at tds.net
Mon Jun 12 22:08:57 CEST 2006


doug shawhan wrote:
> I've been looking at datetime and cannot figure out what was a very 
> simple operation with the time module.
> 
> How does one add or subtract 24 (or any number) of hours from a given 
> date and time using the datetime module?

Use a datetime.timedelta:

In [1]: import datetime

In [2]: now = datetime.datetime.now()

In [3]: now
Out[3]: datetime.datetime(2006, 6, 12, 16, 7, 47, 69000)

In [4]: one_day = datetime.timedelta(hours=24)

In [5]: now-one_day
Out[5]: datetime.datetime(2006, 6, 11, 16, 7, 47, 69000)

Kent



More information about the Tutor mailing list