微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将日期转换为两位数TypeError:-:“ int”和“ str”不支持的操作数类型

如何解决将日期转换为两位数TypeError:-:“ int”和“ str”不支持的操作数类型

尝试将秒数转换为dd:hh:mm:ss

num_of_seconds = int(input("Enter number of seconds: "))

days = num_of_seconds // 86400
days = '%02d' % days
hours = (num_of_seconds - (days*86400)) // 3600
hours = '%02d' % hours
minutes = (num_of_seconds - (days*86400) - (hours*3600)) // 60
minutes = '%02d' % minutes
seconds = (num_of_seconds - (days*86400) - (hours*3600) - minutes*60)
seconds = '%02d' % seconds

print(days,":",hours,minutes,seconds)

我希望不同的时间总是用两位数字格式化(例如05:00:00)

解决方法

您可以使用以下代码删除TypeError

seconds = 208920
seconds_in_day = 60 * 60 * 24
seconds_in_hour = 60 * 60
seconds_in_minute = 60


days = seconds // seconds_in_day
hours = (seconds - (days * seconds_in_day)) // seconds_in_hour
minutes = (seconds - (days * seconds_in_day) - (hours * seconds_in_hour)) // seconds_in_minute

left_over_seconds = seconds - days * seconds_in_day - hours * seconds_in_hour - minutes * seconds_in_minute

print(f'{days:02}',":",f'{hours:02}',f'{minutes:02}',f'{left_over_seconds:02}')

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。