Python 将日期时间格式从日期更改为日期时间

Python 将日期时间格式从日期更改为日期时间,python,python-3.x,datetime,Python,Python 3.x,Datetime,我有一个以字符串形式存储的日期列表。我需要将它们转换为此格式%Y%m%d%H:%m:%S%Z 我无法实现这一点,因为存储为字符串的日期没有时间,只有日期。例如,2015年6月12日 下面是我尝试但失败的代码/想法的一次迭代。非常感谢您的任何建议 d = "2015-06-12" x = datetime.datetime.strptime(d, "%Y-%m-%d").date() x = datetime.date(x) + datetime.time(10, 23) prin

我有一个以字符串形式存储的日期列表。我需要将它们转换为此格式%Y%m%d%H:%m:%S%Z

我无法实现这一点,因为存储为字符串的日期没有时间,只有日期。例如,2015年6月12日

下面是我尝试但失败的代码/想法的一次迭代。非常感谢您的任何建议

d = "2015-06-12"

x = datetime.datetime.strptime(d, "%Y-%m-%d").date()    

x =  datetime.date(x) + datetime.time(10, 23)

print(x)
您可以使用:

要以所需格式打印,您可以使用:

但它不会打印时区名称%Z,因为创建的datetime对象没有关于时区的信息。您可以通过提供UTC和时区之间的时间差手动添加:

from datetime import datetime, timezone, timedelta

x = datetime.strptime(d, "%Y-%m-%d").replace(hour=10, minute=23,
                                             tzinfo=timezone(timedelta(hours=6), name="CST"))
x = datetime.strptime(d, "%Y-%m-%d").replace(hour=10, minute=23, 
                                             tzinfo=datetime.utcnow().astimezone().tzinfo)
或设置本地时区:

from datetime import datetime, timezone, timedelta

x = datetime.strptime(d, "%Y-%m-%d").replace(hour=10, minute=23,
                                             tzinfo=timezone(timedelta(hours=6), name="CST"))
x = datetime.strptime(d, "%Y-%m-%d").replace(hour=10, minute=23, 
                                             tzinfo=datetime.utcnow().astimezone().tzinfo)