python-3.x - 如何添加两个时间日期变量?

标签 python-3.x datetime

total = "00:00:00"
# calculating a "lap" , it will never have Y,M,D , but the log contain it.
laptime = str(datetime.strptime(timeend, "%Y-%m-%d %H:%M:%S") - datetime.strptime(timestart, "%Y-%m-%d %H:%M:%S"))

#failing at hiw to add it to "total"
total = datetime.combine(datetime.strptime(total, "%H:%M:%S") + datetime.strptime(laptime, "%H:%M:%S"))

我尝试过合并,但没有成功。 我如何添加(累积)圈速

最佳答案

试试这个

import datetime as datetime
t1 = datetime.strptime("2018-02-01 14:55:27", "%Y-%m-%d %H:%M:%S")
t2 = datetime.strptime("2018-02-01 14:59:24", "%Y-%m-%d %H:%M:%S")

#for difference
r1 = str(t2 - t1) 
#output 
'0:03:57'

#for adding
r2 = str(t2+ (t2-t1))

#output 
'2018-02-01 15:03:21'

关于python-3.x - 如何添加两个时间日期变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48763766/

相关文章:

c# - 在 C# 中使用 DateTime 和 TimeSpan 进行计算

python-3.x - 为什么 GCP Pub/Sub 订阅无法确认消息?

ruby - 如何解决 `DateTime.parse "Feb. 2 9"` 崩溃?

python时间模块: daylight savings

python - 我将如何使用 while 循环,以便如果他们输入一个数字,它会再次询问他们?

php - 将字符串解析为 DateTime 时出错

java - 使用 getParameterValues 将日期从 jsp 传递到 servlet

python - 为什么以下代码无法成功将所需文件移动到所需目录?

Python3错误: AttributeError: module 'urllib' has no attribute 'request'

python - py2exe,可执行文件 : How to pack data into single file?