472,347 Members | 2,257 Online

How to get hours and minutes from 'datetime.timedelta' object?

Hello,
what is the best /easest way how to get number of hours and minutes
from a timedelta object?
Let's say we have
aa=datetime.datetime(2006, 7, 29, 16, 13, 56, 609000)
bb=datetime.datetime(2006, 8, 3, 17, 59, 36, 46000)
so
c=bb-aa
will be
datetime.timedelta(5, 6339, 437000)

I can easily get days ( c.days)
but
I still can not figure out how easily to get hours and minutes
Any idea?
Thank you for help
L.

Aug 7 '06 #1
5 28179
Hello,
what is the best /easest way how to get number of hours and minutes
from a timedelta object?
Let's say we have
aa=datetime.datetime(2006, 7, 29, 16, 13, 56, 609000)
bb=datetime.datetime(2006, 8, 3, 17, 59, 36, 46000)
so
c=bb-aa
will be
datetime.timedelta(5, 6339, 437000)

I can easily get days ( c.days)
but
I still can not figure out how easily to get hours and minutes
Any idea?

WTF^H^H^H ... You got an answer to this question 5 days ago .....
8<-------------------------------
Sybren Stuvel wrote:
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)

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

8<----------------------------

Aug 7 '06 #2

John Machin wrote:
Hello,
what is the best /easest way how to get number of hours and minutes
from a timedelta object?
....
>diff.days
0
>diff.seconds
52662
>diff.microseconds
922000
>minutes = (diff.seconds + diff.microseconds / 1000000.0) / 60.0
minutes
877.71536666666668
I suspect what Lad wanted was something more like:
>>def secs_mins_hours(timed):
.... total_secs = timed.seconds
.... secs = total_secs % 60
.... total_mins = total_secs / 60
.... mins = total_mins % 60
.... hours = total_mins / 60
.... return (secs, mins, hours)
>>aa=datetime.datetime(2006, 7, 29, 16, 13, 56, 609000)
bb=datetime.datetime(2006, 8, 3, 17, 59, 36, 46000)
td = aa - bb
secs_mins_hours(td)
(39, 45, 1)

I'm surprised that the timedelta class hasn't got secs, mins and hours
properties - they could be generated on the fly in a similar way.

Aug 7 '06 #3

Ant wrote:
John Machin wrote:
Hello,
what is the best /easest way how to get number of hours and minutes
from a timedelta object?
...
>>diff.days
0
>>diff.seconds
52662
>>diff.microseconds
922000
>>minutes = (diff.seconds + diff.microseconds / 1000000.0) / 60.0
>>minutes
877.71536666666668

I suspect what Lad wanted was something more like:
1. If that's what he wanted, it was a very peculiar way of asking. Do
you suspect that he needs to be shown how to conver 877.7... minutes
into hours, minutes and seconds???

2. Please consider that the order of the result would be more
conventionally presented as (hours, minutes, seconds) -- or do you
suspect that the OP needs it presented bassackwards?
>
>def secs_mins_hours(timed):
... total_secs = timed.seconds
... secs = total_secs % 60
... total_mins = total_secs / 60
... mins = total_mins % 60
... hours = total_mins / 60
... return (secs, mins, hours)
>aa=datetime.datetime(2006, 7, 29, 16, 13, 56, 609000)
bb=datetime.datetime(2006, 8, 3, 17, 59, 36, 46000)
td = aa - bb
secs_mins_hours(td)
(39, 45, 1)

I'm surprised that the timedelta class hasn't got secs, mins and hours
properties - they could be generated on the fly in a similar way.
Aug 7 '06 #4

John Machin wrote:
....
1. If that's what he wanted, it was a very peculiar way of asking. Do
you suspect that he needs to be shown how to conver 877.7... minutes
into hours, minutes and seconds???
Chill dude, It wasn't an attack :-)

The datetime class has hour, minute and second attributes that give the
values of each as being in range(24) (hours) and range(60). i.e.
integers. So an educated guess leads me to the conclusion that it is
similar functionality that he wants from the timedelta class.
2. Please consider that the order of the result would be more
conventionally presented as (hours, minutes, seconds) -- or do you
Very good point. That would have been a tricky issue for the OP, and
for that I apologise.
suspect that the OP needs it presented bassackwards?
I think that you have that last word muddled. Not quite ass-backward,
but close ;-)

Aug 7 '06 #5

Ant wrote:
John Machin wrote:
...
1. If that's what he wanted, it was a very peculiar way of asking. Do
you suspect that he needs to be shown how to conver 877.7... minutes
into hours, minutes and seconds???

Chill dude, It wasn't an attack :-)
I didn't think it was.
>
The datetime class has hour, minute and second attributes that give the
values of each as being in range(24) (hours) and range(60). i.e.
integers. So an educated guess leads me to the conclusion that it is
similar functionality that he wants from the timedelta class.
2. Please consider that the order of the result would be more
conventionally presented as (hours, minutes, seconds) -- or do you

Very good point. That would have been a tricky issue for the OP, and
for that I apologise.
suspect that the OP needs it presented bassackwards?

I think that you have that last word muddled. Not quite ass-backward,
but close ;-)
On the contrary. It means "ass-backward, and then some". Google
"dictionary bassackwards" and read the first few hits.

Cheers,
John

Aug 7 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

 14 by: Paul Moore | last post by: I was just writing some code which did date/time manipulations, and I found that the Python 2.3 datetime module does not supply a number of fairly... 16 by: Donnal Walter | last post by: I was very surprised to discover that >>> import datetime >>> x = datetime.date(2004, 9, 14) >>> y = datetime.datetime(2004, 9, 14, 6, 43, 15)... 2 by: Samuel | last post by: Hello, I am trying to convert a local time into UTC ISO8601, then parse it back into local time. I tried the following: ----------------------... 1 by: James | last post by: I need to import a bunch of data into our database for which there's a single entry each day which occurs at the same time every day in local time... 4 by: Shaun | last post by: Hi, I have a table called Bookings which has two important columns; Booking_Start_Time and Booking_End_Time. These columns are both of type... 2 by: Randall Parker | last post by: I know I could just get the year,month,day, etc of of each DateTime vars and calc the hours difference between them. I've done this before in other... 1 by: serge | last post by: Right now the database I am working with is storing time in an Integer data type and is storing the time value in seconds. The application does not... 2 by: skip | last post by: Running from Subversion, I see confusing (to me) behavior related to division of datetime.timedelta objects by integers: % python Python 2.6a0... 18 by: Dirk Hagemann | last post by: Hello, From a zone-file of a Microsoft Active Directory integrated DNS server I get the date/time of the dynamic update entries in a format,... 0 by: teenabhardwaj | last post by: How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of... 0 by: Kemmylinns12 | last post by: Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and... 0 by: Naresh1 | last post by: What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge... 0 by: jalbright99669 | last post by: Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made... 0 by: antdb | last post by: Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was... 0 by: Matthew3360 | last post by: Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ... 2 by: Matthew3360 | last post by: Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it... 0 by: WisdomUfot | last post by: It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific... 0 by: Oralloy | last post by: Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...