Subtract time from datetime.time object
1 2 3 4 | from datetime import datetime, timedelta current_time = datetime.now().time() new_time = current_time - timedelta(seconds=10) >> TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.timedelta' |
如何从datetime.time对象中减去时间? 此代码似乎仅适用于datetime.datetime对象。
Timedelta仅适用于datetime.datetime和datetime.date对象。 但你总能这样做
1 2 3 | current_dt = datetime.datetime.now() newdt = currentdt - datetime.delta(seconds=10) new_time = newdt.time() |
在返回时间之前,您可以从当前
1 | current_time = (datetime.now() - timedelta(seconds=10)).time() |
在datetime减法后获取时间:
1 2 3 4 5 | from datetime import datetime, timedelta current_time = datetime.now() new_time = current_time - timedelta(seconds=10) new_time = new_time.time() print new_time |
输出:10:56:43.408337