Convert number to hours in python

Asked

Viewed 587 times

-1

How to convert a 1234 number representing houras to a string with time format e.g. 12:34 in python?

  • How to convert 5 to 00:05?

  • 3

    What if it’s 1265? There’s no turning back 12:65.

  • This number represents a time or a number of hours/minutes?

2 answers

0

I did as follows for situations where the input number is for example 5

 x_fill = str(x).zfill(4)
result = str(x_fill)[:2]+':'+str(x_fill)[2:]

-1

def horas(number):
    return str(number)[:2]+':'+str(number)[2:]

horas(1234)

Output: '12:34'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.