calculate difference between two timestamps [newbie]

Dave Angel d at davea.name
Sat Dec 17 06:09:50 EST 2011


On 12/17/2011 05:19 AM, nukeymusic wrote:
> I'm trying to calculate the difference in seconds between two
> timestamps, but I'm totally stuck:
> date1="Dec-13-09:47:12"
> date2="Dec-13-09:47:39"
>>>> diff=datetime.date(date2)-datetime.date(date1)
> Traceback (most recent call last):
>    File "<stdin>", line 1, in<module>
> TypeError: an integer is required
>
> struct_date1=time.strptime(date1, "%b-%d-%H:%M:%S")
> struct_date2=time.strptime(date2, "%b-%d-%H:%M:%S")
>>>> diff=datetime.date(struct_date2)-datetime.date(struct_date1)
> Traceback (most recent call last):
>    File "<stdin>", line 1, in<module>
> TypeError: an integer is required
>
> thanks in advance
> nukey
You should post the full code;  you omitted your import line(s)

That last approach was closer, try this one:

import datetime
struct_date1=datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S")
struct_date2=datetime.datetime.strptime(date2, "%b-%d-%H:%M:%S")
diff = struct_date2 - struct_date1
print diff

diff is of type  datetime.timedelta

-- 

DaveA




More information about the Python-list mailing list