2 回答
TA贡献1824条经验 获得超8个赞
用途combine
:
import datetime as dt
sTime = "13:00"
sTime = dt.datetime.strptime(sTime, '%H:%M')
print(dt.datetime.combine(dt.datetime.today(), sTime.time()))
输出
2020-08-28 13:00:00
或者直接但需要做更多的工作:
import datetime as dt
sTime = "13:00"
sTime = dt.datetime.strptime(sTime, '%H:%M')
today = dt.datetime.today()
print(dt.datetime(today.year, today.month, today.day, sTime.hour, sTime.minute, sTime.second))
TA贡献1865条经验 获得超7个赞
import datetime as dt
sTime = "13:00"
# PARSE string to datetime object
objDateTime = dt.datetime.strptime(sTime, "%H:%M")
# combine PARSED time from sTime with today's date
objDateTime = dt.datetime.combine(dt.datetime.today(), objDateTime.time())
# FORMAT objDateTime to Hr:Min string output
print(objDateTime.strftime("%H:%M"))
# FORMAT objDateTime to DD-MMM-YYYY Hr:Min string output
print(objDateTime.strftime("%d-%b-%Y %H:%M"))
输出(2020 年 8 月 28 日运行时)
13:00
28-Aug-2020 13:00
添加回答
举报