TypeError:unsupported format string passed to Series.__format__

TypeError:unsupported format string passed to Series.__format__

我正在尝试以 12 小时制格式打印时间,但 Django 抛出 TypeError。 python 工作正常,但在 django 中显示错误 谢谢提前

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

就像我说的那样,使用 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