我有一个日期字符串,想把它转换成日期类型:
我已尝试将 datetime.datetime.strptime
与我想要的格式一起使用,但它正在返回转换时间。
when = alldates[int(daypos[0])]
print when, type(when)
then = datetime.datetime.strptime(when, '%Y-%m-%d')
print then, type(then)
这是输出返回的内容:
2013-05-07 <type 'str'>
2013-05-07 00:00:00 <type 'datetime.datetime'>
我需要去掉时间:00:00:00
.
最佳答案
print then.date()
你想要的是一个 datetime.date 对象。你所拥有的是一个 datetime.datetime 对象。您可以按上述方式在打印时更改对象,也可以在创建对象时执行以下操作:
then = datetime.datetime.strptime(when, '%Y-%m-%d').date()
关于python - 显示没有时间的 Python 日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26153795/