Python - 从Datetime字符串移除时间 [英] Python -Remove Time from Datetime String
本文介绍了Python - 从Datetime字符串移除时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经尝试使用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()
I have a date string and want to convert it to the date type:
I have tried to use datetime.datetime.strptime with the format that I want but it is returning the time with the conversion.
when = alldates[int(daypos[0])]
print when, type(when)
then = datetime.datetime.strptime(when, '%Y-%m-%d')
print then, type(then)
This is what the output returns:
2013-05-07 <type 'str'>
2013-05-07 00:00:00 <type 'datetime.datetime'>
I need to remove the the time: 00:00:00.
解决方案
print then.date()
What you want is a datetime.date object. What you have is a datetime.datetime object. You can either change the object when you print as per above, or do the following when creating the object:
then = datetime.datetime.strptime(when, '%Y-%m-%d').date()
这篇关于Python - 从Datetime字符串移除时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文