我试图以12小时时钟格式打印时间,但django抛出TypeError。在python中工作正常,但在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)
返回10:45am
就像我说的,使用strftime来做这些繁琐的工作,而不是自己做。
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