[Tutor] I need a time object from the future

Kent Johnson kent37 at tds.net
Fri Oct 20 20:37:49 CEST 2006


William O'Higgins Witteman wrote:
> I've been looking into this, and I am not understanding how to get this
> task done.  I need to be able to look at a time object and know if it si
> between now and a set point 120 days in the future.  I can get a value
> for now (currently I am using datetime.datetime.now()), but I haven't
> found a way to get now + 120 days, or figured out how to test if a given
> datetime falls between the two.  Any pointers would be appreciated.
> Thanks.

You can do this with datetime.timedelta:

In [1]: from datetime import datetime, timedelta

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

In [3]: n
Out[3]: datetime.datetime(2006, 10, 20, 14, 36, 44, 453000)

In [4]: later = n + timedelta(days=120)

In [5]: later
Out[5]: datetime.datetime(2007, 2, 17, 14, 36, 44, 453000)

In [6]: n < later
Out[6]: True

Kent



More information about the Tutor mailing list