Home > database >  Convert 24 Hour values into HHMMSS - Time
Convert 24 Hour values into HHMMSS - Time

Time:10-13

How do I convert integer hour type values like 1700, 2400, 1000 into timestamps 17:00:00, 00:00:00, 10:00:00?

import time
hour = int(1700)
print(time.strftime('%H:%M:%S', time.gmtime(hour)))
Out[1]: '00:28'

or

data = pd.DataFrame({'Hour':[1700, 2400, 1000]})
print(time.strftime('%H:%M:%S', time.gmtime(data.Hour)))

TypeError: cannot convert the series to <class 'int'>

CodePudding user response:

Convert the integer hour to a string which you can parse and format using datetime.strptime and datetime.strftime respectively.

Demo:

from datetime import datetime

x = 1700
dt = datetime.strptime(str(x), '%H%M')
formatted_str = datetime.strftime(dt, '%H:%M:%S')
print(formatted_str)

Output:

17:00:00
  • Related