473,545 Members | 2,095 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Lad
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.dat etime(2006, 7, 29, 16, 13, 56, 609000)
bb=datetime.dat etime(2006, 8, 3, 17, 59, 36, 46000)
so
c=bb-aa
will be
datetime.timede lta(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 28424
Lad wrote:
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.dat etime(2006, 7, 29, 16, 13, 56, 609000)
bb=datetime.dat etime(2006, 8, 3, 17, 59, 36, 46000)
so
c=bb-aa
will be
datetime.timede lta(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 .....
[thread copied below]
8<-------------------------------
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.date time(2006, 8, 2, 8, 57, 28, 687000)
a=datetime.date time(2006, 8, 1, 18, 19, 45, 765000)
diff = b - a
Ok, I tried
>diff=b-a
diff
datetime.timede lta(0, 52662, 922000)
>diff.min
datetime.timede lta(-999999999)
Reread the manual:

1. "min" is minIMUM, not minUTES

2. You need:
>>diff.days
0
>>diff.second s
52662
>>diff.microsec onds
922000
>>minutes = (diff.seconds + diff.microsecon ds / 1000000.0) / 60.0
minutes
877.71536666666 668

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

Aug 7 '06 #2
Ant

John Machin wrote:
Lad 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.microseco nds
922000
>minutes = (diff.seconds + diff.microsecon ds / 1000000.0) / 60.0
minutes
877.71536666666 668
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.d atetime(2006, 7, 29, 16, 13, 56, 609000)
bb=datetime.d atetime(2006, 8, 3, 17, 59, 36, 46000)
td = aa - bb
secs_mins_hou rs(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:
Lad wrote:
Hello,
what is the best /easest way how to get number of hours and minutes
from a timedelta object?
...
>>diff.days
0
>>diff.second s
52662
>>diff.microsec onds
922000
>>minutes = (diff.seconds + diff.microsecon ds / 1000000.0) / 60.0
>>minutes
877.71536666666 668

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.da tetime(2006, 7, 29, 16, 13, 56, 609000)
bb=datetime.da tetime(2006, 8, 3, 17, 59, 36, 46000)
td = aa - bb
secs_mins_hour s(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
Ant

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
6406
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 basic functions. I understand the reasoning (the datetime module provides a set of datatypes, and doesn't attempt to get into the murky waters of date algorithms) but as these things can be quite...
16
13102
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) >>> print x == y True How can these two objects be considered equal? Is there a *general* way to test for date != datetime as well as 4.5 != 4.6?
2
3719
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: ---------------------- #!/usr/bin/python import time import datetime import xml.utils.iso8601
1
3788
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 - so I need to convert this to UTC taking into account local daylight savings. However daylight savings just don't seem to be working at all... Python 2.3.5 (#2, May 4 2005, 08:51:39) on...
4
2889
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 DATETIME. Given any day how can I calculate how many hours are available between the hours of 09.00 and 17.30 so a user can see at a glance how many hours they have unbooked on a particular day (i.e....
2
10994
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 languages with a formula for handling leap years. But has someone already done what is necessary to get the number of seconds or minutes or hours between two DateTimes? Can one convert to ticks,...
1
7405
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 allow entering seconds. It accepts minutes and hours. I have a report where it is doing: SELECT SUM(TIMEENTERED) and the SUM is *blowing* up as the SUM is reaching
2
4287
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 (trunk:57277:57280M, Aug 28 2007, 17:44:49) on darwin Type "help", "copyright", "credits" or "license" for more information. Traceback (most recent call last): File "<stdin>", line 1, in <module>...
18
13021
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, which is as far as I know the hours since january 1st 1901. For Example: the number 3566839 is 27.11.07 7:00. To calculate this in Excel I use this:...
0
7464
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7396
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7805
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7413
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7751
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5323
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4943
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3440
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.