How to find out a date/time difference

Nico Grubert nicogrubert at gmail.com
Wed May 24 10:29:27 EDT 2006



 > I use  datetime class in my program and now
 > I have two fields that have the datetime format like this
 > datetime.datetime(2006, 5, 24, 16, 1, 26)

 > How can I find out the date/time difference ( in days) of such two
 > fields?

Hi Lad,

you could do this:

 >>> a = datetime.datetime(2006, 5, 24, 16, 1, 26)
 >>> b = datetime.datetime(2006, 5, 20, 12, 1, 26)
 >>> a-b
datetime.timedelta(4)
# 4 days

 >>> b = datetime.datetime(2006, 5, 20, 12, 1, 26)
 >>> x = a-b
 >>> x
datetime.timedelta(4, 14400)
 >>> str(x)
'4 days, 4:00:00'


Regards,
Nico



More information about the Python-list mailing list