【问题标题】:Input time twice as a string and calculate the difference [duplicate]输入时间两次作为字符串并计算差异[重复]
【发布时间】:2014-11-20 02:32:52
【问题描述】:

我正在尝试输入一个字符串,例如"10:20",然后输入第二个字符串,例如"10:25",并计算差值。

我发现 Python 中的 time 函数难以理解。

任何指点将不胜感激。

【问题讨论】:

  • 看看datetime,特别是strptime
  • 当我们不知道您觉得难以理解时很难回答
  • 希望你能在这里找到答案link

标签: python


【解决方案1】:

您可以使用datetime 模块。这是优雅的 Pythonic 方式!

from datetime import datetime
s1 = '10:20'
s2 = '11:25' 
tformat = '%H:%M' # specify the format of your time string
diftime = datetime.strptime(s2, tformat) - datetime.strptime(s1, tformat)

print diftime
1:05:00

【讨论】:

    猜你喜欢
    • 2019-03-04
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-21
    • 1970-01-01
    相关资源
    最近更新 更多