I am trying to print time in 12 hour clock format but django throws TypeError. works fine for python but shows error in django Thanks is Advance
def solve(s, n):
h, m = map(int, s[:-2].split(':'))
h =h% 12
if s[-2:] == 'pm':
h =h 12
t = h * 60 m n
h, m = divmod(t, 60)
h =h% 24
if h < 12:
suffix = 'a'
else:
suffix='p'
h =h% 12
if h == 0:
h = 12
return "{:02d}:{:02d}{}m".format(h, m, suffix)
solve('10:00am',45)
returns 10:45am
CodePudding user response:
Like I said, use strftime to do this dirty work, instead of doing it on your own.
from datetime import datetime, timedelta
def solve(s, minutes):
time_instance = datetime.strptime(s, '%I:%M%p')
time_instance = timedelta(minutes=minutes)
return time_instance.strftime('%I:%M%p').lower()
print(solve('10:00am', 45)) # 10:45am
print(solve('10:00pm', 45)) # 10:45pm
print(solve('11:30pm', 45)) # 12:15am
print(solve('11:30pm', 120)) # 01:30am