我正在尝试运行将计算当前时间与午夜之间的时差的代码,并且最终输出应仅以“%H:%M:%S”格式打印。它运行正常,只是它还在输出中添加了1900-01-01日期。我不确定该如何解决。
import os
from datetime import date
from datetime import time
import datetime as dt
time_current = (str(dt.datetime.now().time()))
hour = (int(time_current[:-13]))
minute = (int(time_current[3:-10]))
second = (int(time_current[6:-7]))
new_hour = str(23 - hour)
new_minute = str(59 - minute)
new_second = str(59 - second)
new_time = (new_hour + ":" + new_minute + ":" + new_second)
time_left = dt.datetime.strptime(new_time,"%H:%M:%S")
print (time_left)
答案 0 :(得分:0)
datetime.datetime
对象始终具有日期部分。
如果只想打印时间部分,只需将最后一行替换为print (time_left.time())