Answers for "python hour conversion"

22

python hour from date

import datetime
date = '2021-05-21 11:22:03'
datem = datetime.datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
print(datem.day)        # 25
print(datem.month)      # 5
print(datem.year)       # 2021
print(datem.hour)       # 11
print(datem.minute)     # 22
print(datem.second)     # 3
Posted by: Guest on May-21-2021
0

decimal hour to hour minute python

>>> def frac(n):
...     i = int(n)
...     f = round((n - int(n)), 4)
...     return (i, f)
...
>>> frac(53.45)
(53, 0.45) # A tuple 

>>> def frmt(hour): # You can rewrite it with 'while' if you wish
...     hours, _min = frac(hour)
...     minutes, _sec = frac(_min*60)
...     seconds, _msec = frac(_sec*60)
...     return "%s:%s:%s"%(hours, minutes, seconds)
...
>>> frmt(72.345)
'72:20:42'
>>> l = [72.345, 72.629, 71.327]
>>> map(frmt, l)
['72:20:42', '72:37:44', '71:19:37']
Posted by: Guest on June-16-2020

Python Answers by Framework

Browse Popular Code Answers by Language