(datetime.datetime.strptime)并不需要1900-01-01日期

时间:2019-10-19 17:00:10

标签: python-3.x datetime strptime

我正在尝试运行将计算当前时间与午夜之间的时差的代码,并且最终输出应仅以“%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)

1 个答案:

答案 0 :(得分:0)

datetime.datetime对象始终具有日期部分。

如果只想打印时间部分,只需将最后一行替换为print (time_left.time())