python django减去时间字段

时间:2015-09-08 11:46:27

标签: python django

我有两次NaNstart_time

对于开始时间,我收到end_time,而对于end_time,我收到6:30。 现在我只想减去时间。我从html文本字段中得到这个。

如何减去这两个时间字段?

提前致谢

2 个答案:

答案 0 :(得分:2)

>>> import datetime
>>> time1 = datetime.datetime.strptime("6:30",'%H:%M')
>>> time2 = datetime.datetime.strptime("7:30",'%H:%M')
>>> difference = time2-time1
>>> difference
datetime.timedelta(0, 3600)
>>> difference.seconds
3600
>>> "{:0>8}".format(datetime.timedelta(seconds=difference.seconds))
'01:00:00'
>>> "{:0>8}".format(datetime.timedelta(seconds=666777))
'7 days, 17:12:57'

答案 1 :(得分:0)

>>> from datetime import datetime
>>> s1 = "06:30"
>>> s2 = "07:30"
>>> timeformat = "%I:%M"
>>> delta = datetime.strptime(s2, timeformat) - datetime.strptime(s1, timeformat)
>>> delta
datetime.timedelta(0, 3600)
>>> delta.seconds
3600

delta是时差。您可以检查值并相应地进行转换。