[Zope] Subtracting 2 dates from each other

Tino Wildenhain tino@wildenhain.de
Mon, 28 May 2001 17:18:27 +0200


Hi Gitte,

--On Montag, 28. Mai 2001 15:16 +0200 Gitte Wange <gitte@mmmanager.org> 
wrote:

> On Monday 28 May 2001 15:18, Peter Bengtsson wrote:
>> What do you really want?
>>
>> >>> from DateTime import DateTime
>> >>> date1 = DateTime('2001/05/25 20:30:00')
>> >>> date2 = DateTime('2001/05/25 19:00:00')
>> >>> res = date1 - date2
>> >>> type(res)
>>
>> <type 'float'>
>>
>> >>> hours = res*24
>> >>> type(hours)
>>
>> <type 'float'>
>>
>> >>>  print hours
>>
>> 1.5
>>
>> Do you want to calculate how many hours there are between two dates? Or
>> how many days (like 'res' above)?
>> If your result is unchanging even though you change the dates, could it
>> be because you're doing some int() roundoff or something so that you
>> loose the
>>
>> accuracy in the hours; like this:
>> >>> print int(res*24)
>>
>> 1
>>
>> Peter
>
> I want to know EXTACTLY how long time (in hours, minutes, seconds) there
> is  between Date1 and Date2 ...
>
> Anyone ???

I've checked it and Peter is right, the difference between two
DateTime objects is given in float. (meaning the days)

So all you need is a little math ;-)

d=Date1-Date2

hours=int(d*24.0) % 24
minutes=int(d*24.0*60.0) % 60
seconds=int(d*24.0*60.0*60.0) % 60

Regards
Tino