python3上的python hh:mm:ss到秒

2024-06-16 12:10:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图写一个程序,检查一条道路的平均速度。这个程序将在这里输入两次1-2点。然后程序计算出差异。这一切都有效。我唯一需要的是HH:MM:SS的差分输出,我需要它在几秒钟或几分钟内。这将收集从差异中转换的时间。到目前为止,我的代码是:

from datetime import datetime
#start
print ("Welcome to This Speed Check!")

#varibles
distance=int(input("How far did you travel!(Miles)"))
time1=input("Whats was the time you went past the first point(H:M:S)?")
time2=input("What was the finishing time?")
FMT = '%H:%M:%S'
diffrence = datetime.strptime(time2, FMT) - datetime.strptime(time1, FMT)
print(diffrence)

这一切都起作用了,给了我我想要的,我需要把差异转换成秒

谢谢亚当


Tags: the程序youinputdatetimetime差异print
3条回答

您只需调用seconds

print(diffrence.seconds)

由于diffrence^{}类型,您应该将^{}方法用作:

 print(diffrence.total_seconds())

这将为您提供以秒为单位的时间差:

print(diffrence.total_seconds())

示例:

^{pr2}$

输出:

31536000.0

看起来不错:

print(365 * 24 * 60 * 60)

31536000

docs

Total seconds in the duration.

相关问题 更多 >