Datetime objects

John Machin sjmachin at lexicon.net
Wed Aug 2 09:03:01 EDT 2006


Lad wrote:
> Sybren Stuvel wrote:
> > Lad enlightened us with:
> > > How can I find days and minutes difference between two datetime
> > > objects?
> > > For example If I  have
> > > b=datetime.datetime(2006, 8, 2, 8, 57, 28, 687000)
> > > a=datetime.datetime(2006, 8, 1, 18, 19, 45, 765000)
> >
> > diff = b - a
>
> Ok, I tried
>
> >>> diff=b-a
> >>> diff
> datetime.timedelta(0, 52662, 922000)
> >>> diff.min
> datetime.timedelta(-999999999)

Reread the manual:

1. "min" is minIMUM, not minUTES

2. You need:
>>> diff.days
0
>>> diff.seconds
52662
>>> diff.microseconds
922000
>>> minutes = (diff.seconds + diff.microseconds / 1000000.0) / 60.0
>>> minutes
877.71536666666668
>>>
>
>
> which is not good for me.
>
> So I tried to use toordinal like this
> diff=b.toordinal()-a.toordinal()
>
> but I get
> diff=1
>
> Why?

because toordinal() works only on the date part, ignoring the time
part.

HTH,
John




More information about the Python-list mailing list