Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个在 %I:%M%p 中给出的输入(例如“6:02PM”)。我正在尝试输入此代码以找出现在和那时之间的区别:
import datetime now = datetime.now() then = "6:02PM" tdelta = now - then
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