3

我有一个在 %I:%M%p 中给出的输入(例如“6:02PM”)。我正在尝试输入此代码以找出现在和那时之间的区别:

import datetime

now = datetime.now()
then = "6:02PM"
tdelta = now - then
4

1 回答 1

3
import datetime as dt

now = dt.datetime.now()
then = dt.datetime.combine(now, dt.datetime.strptime("6:02PM", "%I:%M%p").time())
print(then)
# 2012-08-26 18:02:00

tdelta = now - then
print(tdelta)
# -1 day, 20:53:25.190721
于 2012-08-26T18:56:03.697 回答