【发布时间】:2017-01-26 05:33:13
【问题描述】:
这是我的代码:
from datetime import datetime
def get_local_time(time_str):
"""
takes a string in the format of '27 March at 3:00' which is UTC
and converts it to local time and AM/PM
:param time_str:
"""
offset = datetime.now() - datetime.utcnow()
time_dt = datetime.strptime(time_str, '%d %b at %H:%M')
return (time_dt + offset).strftime('%I:%M %p')
我遇到的问题是使用 time_str,它只是时间,不包括日/月。即:“02:00”
如果我将其更改为:time_dt = datetime.strptime(time_str, '%H:%M') 那么我会收到关于 strftime 和 1900 年之前的年份的错误。
所以我被难住了。需要做什么才能在输入字符串中只允许一个时间?
【问题讨论】:
标签: python python-2.7 python-datetime