[Tutor] A question about: Adding seconds to datetime object

Kent Johnson kent37 at tds.net
Sat Nov 18 15:48:27 CET 2006


Asrarahmed Kadri wrote:
> Hi ,
>  
>  
> I have a question:
>  
> Is it possible to add seconds to a datetime object and get the result as 
> a new datetime object. I mean when we keep adding, for example, 3600 
> seconds, the date will get changed after 24 iterations. Is it possible 
> to carry out such an operation ?

Sure, just add a datetime.timedelta to the datetime object:

In [1]: from datetime import datetime, timedelta

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

In [3]: d
Out[3]: datetime.datetime(2006, 11, 18, 9, 47, 31, 187000)

In [4]: delta=timedelta(seconds=3600)

In [5]: d+delta
Out[5]: datetime.datetime(2006, 11, 18, 10, 47, 31, 187000)

Kent



More information about the Tutor mailing list