views:

171

answers:

4

I must be doing something wrong here, any ideas?

>>> (datetime.datetime(2008,11,7,10,5,14)-datetime.datetime(2008,11,6,9,30,16)).seconds
2098

It should be getting more than that many seconds.

+1  A: 

timedelta.seconds isn't the total number of seconds, it's the remainder in seconds after days have been accounted for. Using your example:

>>> import datetime
>>> (datetime.datetime(2008,11,7,10,5,14)-datetime.datetime(2008,11,6,9,30,16))
datetime.timedelta(1, 2098)

That datetime.timedelta(1, 2098) means that your timedelta is 1 day, plus 2098 seconds.

What you want is something like:

>>> delta = (datetime.datetime(2008,11,7,10,5,14)-datetime.datetime(2008,11,6,9,30,16))
>>> (delta.days * 86400) + delta.seconds
88498
Daniel Pryden
+7  A: 

timedelta.seconds gives you the seconds field of the timedelta. But it also has a days field (and a milliseconds field).

So you would want something like

delta = datetime.datetime(2008,11,7,10,5,14)-datetime.datetime(2008,11,6,9,30,16)
delta.seconds + delta.days*86400
Artelius
+2  A: 

It's actually returning a timedelta which has a day field also i.e.

c.seconds = 2098

but

c.days = 1

Strawberry
+1  A: 

datetime.datetime(2008,11,7,10,5,14)-datetime.datetime(2008,11,6,9,30,16) returns a datetime.timedelta object which has a days attribute. The difference that you are calculating is actually 1 day and 2098 seconds.

mhawke