Converting minutes to HH:MM format in Python [duplicate] - python

This question already has answers here:
How get hours:minutes
(4 answers)
Closed 9 years ago.
First of all, I'd like to point out that I'm a beginner with Python.
My problem is that I can't figure out what is the proper way to convert minutes to HH:MM format in Python.
Any help is appreciated!

Use the divmod() function:
'{:02d}:{:02d}'.format(*divmod(minutes, 60))
Here divmod() divides the minutes by 60, returning the number of hours and the remainder, in one.
Demo:
>>> minutes = 135
>>> '{:02d}:{:02d}'.format(*divmod(minutes, 60))
'02:15'

Related

Python timedelta class minutes [duplicate]

This question already has answers here:
Convert a timedelta to days, hours and minutes
(14 answers)
Closed 2 years ago.
How to get timedelta's minutes and seconds(ex: 2:01)? Is there any formula for this?
I've tried this, but it really didn't work well
minutes = int(diff.seconds/60)
Isayahs answer works great, but if you don't want to use any libraries you could do something similar to this:
minutes = seconds // 60
remainingSeconds = seconds % 60
print(str(minutes) + ":" + str(remainingSeconds))
I may be misunderstanding the question but if you are looking for a timedelta object where seconds = 121 then:
>>> from datetime import timedelta
>>> timedelta(minutes=2, seconds=1)
datetime.timedelta(seconds=121)

Convert '2019-10-16' do date object [duplicate]

This question already has answers here:
Parse date string and change format
(10 answers)
Closed 3 years ago.
I realize this might be the most well-documented thing on the Internet but I can't seem to get it right. I have a string, '2019-10-16' that I want to turn into a date object so I can increase it incrementally, but can still be converted to the string '2019-10-06' again. However, I seem to only be able to get it as 2019.10.16 or something similar.
import datetime
day = '2019-10-16'
date_object = datetime.datetime.strptime(day, '%Y-%m-%d')
>date_object
>datetime.datetime(2019, 10, 16, 0, 0)
To change it use date_object.strftime('%Y-%m-%d')

Time difference in microseconds not working as expected [duplicate]

This question already has answers here:
Python - time difference in milliseconds not working for me
(5 answers)
Python speed testing - Time Difference - milliseconds
(16 answers)
Closed 3 years ago.
I'm trying to get difference between two datetimes, but I don't know why I'm getting 0 when trying to get microseconds:
from dateutil.parser import parse
x = parse("2019-03-25T17:33:08.829-03:00")
y = parse("2019-03-25T18:07:08.829-03:00")
result = y - x
print(result.microseconds) // prints 0
Tried:
Python - time difference in milliseconds not working for me
and
Python speed testing - Time Difference - milliseconds
with no luck.
What I'm doing wrong here?
One of the answers of the posts you linked says:
Be aware that c.microseconds only returns the microseconds portion of the timedelta! For timing purposes always use c.total_seconds().
If you want the microseconds portion, what else did you expect? The fractional part of the seconds of both your dates are equal, so the difference is 0.
Otherwise, use result.total_seconds() * 1e6 + result.microseconds.
You did not compute the difference in microseconds. Rather, you found the time difference of 34 minutes, and asked for the microseconds component of that difference. The time difference is 0:34:00. Of this figure, every component except minutes is 0.
To see this effect, insert this simple tracing code into your program:
print(result, type(result))
print(x, type(x))
print(y, type(y))
Output:
2019-03-25 17:33:08.829000-03:00 <class 'datetime.datetime'>
2019-03-25 18:07:08.829000-03:00 <class 'datetime.datetime'>
0:34:00 <class 'datetime.timedelta'>
You need to take the entire timedelta and convert it to microseconds. Now that you see the problem, I'll bet you can fix it on your own. :-)

How to string format a float to the 10s or 100s place? [duplicate]

This question already has answers here:
How to round a number to significant figures in Python
(26 answers)
Closed 6 years ago.
How do I string format, either % or .format(), a float to round and display to the 10s or 100s place?
Like 4552.33 to 4550 to 10s place or 4600 to 100s?
Use the built-in function round,
>>> import math
>>> f = 4552.33
>>> int(round(f, -int(math.log10(10))))
4550
>>> int(round(f, -int(math.log10(100))))
4600

python datetime.timedelta - how to display in form <seconds>.<micros> [duplicate]

This question already has answers here:
How can I format timedelta for display
(6 answers)
Closed 6 years ago.
If I have a datetime.timedelta of:
datetime.timedelta(0, 0, 66)
print my_dt
0:00:00.000066
How can I keep and print just the seconds and the microseconds together? (i.e. strip the mins and hours).
I can see how to take just the micro and or the seconds, i.e. by those objects (e.g. my_dt.microseconds) - but I would like to keep both in the same cmd without any ugly formatting, sticking strings together after the fact.
For this example the output I am after would be:
00.000066
Any help appreciated (python n00b)
Version:
Python 2.6.6
Here is the official documentation for datetime util.
datetime
If you have a timedelta objects, it means that you've have a time interval which is stable. timedelta object has 3 attributes: days, seconds and microseconds.
For example: If your time interval is 1 day, 5 hours, 23 minutes, 10 seconds and 50 microseconds; do you want the output format to be only 10.000050 ?
I'm guessing like the above.
So your code should be like:
seconds = my_dt.seconds%60
microseconds = my_dt.microseconds%1000000
result = "%d.%d" %(seconds,microseconds)

Categories