如何将晚上7点转换为Python Datetime对象

Yax*_*Yax 0 python

我有一个带有start_time列的Schedule表,它以这种格式存储时间,

4:00 PM

下午7时04分

5:00 AM

我希望能够根据用户每天选择的start_time为用户启动活动.

如何将晚上7点转换为datetime对象或实现此目的的最佳方法是什么?

Amb*_*ber 5

# time objects have no strptime method, so use datetime's, but it sets the
# date to 1900-01-01 by default
wrong_date = datetime.datetime.strptime('7:00pm', '%I:%M%p')
# replace 1900-01-01 with the actual date
correct_date = datetime.datetime.combine(datetime.date.today(), wrong_date.time())
# correct_date is now datetime.datetime(2016, 10, 22, 19, 0)
Run Code Online (Sandbox Code Playgroud)