将x秒的时间添加到HH的字符串变量时间:MM

时间:2016-10-27 14:03:56

标签: python python-2.7 datetime strptime python-datetime

我试图找出一种基于在开始时间上添加最少时间来制作新变量AUTOMATIC_END_TIME的方法,但我无法找出允许的方式{ {1}}变成一个可以在其上添加时间的时间。

到目前为止,我的脚本有以下内容:

START_TIME

当前脚本根本不应发生变化,from time import sleep from datetime import datetime, timedelta START_TIME = "19:18" END_TIME = "19:25" LOGA = ["one", "two"] TIME_DIFFERENCE = datetime.strptime(END_TIME, "%H:%M") - datetime.strptime(START_TIME, "%H:%M") TIME_DIFFERENCE = TIME_DIFFERENCE.seconds if len(LOGA) * (60 * (5 + 1)) >= TIME_DIFFERENCE: print "Show minimum end time" AUTOMATIC_END_TIME = "" # Should come out as 19:30 除外AUTOMATIC_END_TIME START_TIME + (60 * (5 + 1)它应该是19:30

2 个答案:

答案 0 :(得分:2)

>>> (datetime.strptime(START_TIME, "%H:%M") + timedelta(minutes=12)).strftime('%H:%M')
'19:30'

答案 1 :(得分:1)

from time import sleep
from datetime import datetime, timedelta

START_TIME = "19:18"
END_TIME = "19:25"

LOGA = ["one", "two"]

TIME_DIFFERENCE = datetime.strptime(END_TIME, "%H:%M") - datetime.strptime(START_TIME, "%H:%M")
TIME_DIFFERENCE = TIME_DIFFERENCE.seconds

if len(LOGA) * (60 * (5 + 1)) >= TIME_DIFFERENCE:
    print "Show minimum end time"
    AUTOMATIC_END_TIME = (datetime.strptime(START_TIME, "%H:%M") + timedelta(minutes=12)).strftime('%H:%M')
    print AUTOMATIC_END_TIME