How to get hours: minutes

I have a script here (not mine) that calculates the length of a movie in my companion. It displays the length in minutes: seconds

I want to have it in hours: minutes

What changes should I make?

This is a world script:

if len > 0:
    len = "%d:%02d" % (len / 60, len % 60)
else:
    len = ""

res = [ None ]

I already have a clock, dividing by 3600 instead of 60, but I can’t get the minutes ...

Thank you in advance

Peter

+3
source share
4 answers
hours = secs / 3600
minutes = secs / 60 - hours * 60

len = "%d:%02d" % (hours, minutes)

Or, for later versions of Python:

hours = secs // 3600
minutes = secs // 60 - hours * 60

len = "%d:%02d" % (hours, minutes)
+8
source

You can use timedelta p>

from datetime import timedelta
str(timedelta(minutes=100))[:-3]
# "1:40"
+8
source

, ? . Python len - . .

def display_movie_length(seconds):
    # the // ensures you are using integer division
    # You can also use / in python 2.x
    hours = seconds // 3600   

    # You need to understand how the modulo operator works
    rest_of_seconds = seconds % 3600  

    # I'm sure you can figure out what to do with all those leftover seconds
    minutes = minutes_from_seconds(rest_of_seconds)

    return "%d:%02d" % (hours, minutes)

, , , minutes\_from\_seconds(). , modulo.

0

There is a good answer here fooobar.com/questions/1723693 / ... (later duplicate of this question)

However, if you are dealing with time zone offsets in a date and time string, you also need to handle negative hours, in which case zero padding in Martijn's answer does not work.

for example, he will return -4:00instead-04:00

To fix this, the code gets a little longer, as shown below:

offset_h, offset_m = divmod(offset_minutes, 60)
sign = '-' if offset_h < 0 else '+'
offset_str = '{}{:02d}{:02d}'.format(sign, abs(offset_h), offset_m)
0
source

Source: https://habr.com/ru/post/1723691/


All Articles