要将字符串'08:50'转换为日期类型,需要指定一个日期作为参考。以下是一个示例代码,将字符串转换为日期类型,并使用当前日期作为参考:

import datetime

time_str = '08:50'
current_date = datetime.date.today()
time_date = datetime.datetime.strptime(time_str, '%H:%M').time()
date_time = datetime.datetime.combine(current_date, time_date)

print(date_time)

输出结果为当前日期加上指定时间的完整日期时间类型。

Python 字符串转日期时间:将 '08:50' 转换为完整日期时间

原文地址: https://www.cveoy.top/t/topic/l1Iq 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录