显示没有时间的Python日期时间 [英] Display Python datetime without time

查看:61
本文介绍了显示没有时间的Python日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期字符串,想将其转换为日期类型:

I have a date string and want to convert it to the date type:

我尝试将datetime.datetime.strptime与所需的格式一起使用,但是它将返回转换的时间.

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)

这是输出返回的内容:

   2013-05-07 <type 'str'>
   2013-05-07 00:00:00 <type 'datetime.datetime'>

我需要删除时间:00:00:00.

I need to remove the the time: 00:00:00.

推荐答案

print then.date()

您想要的是一个datetime.date对象.您拥有的是一个datetime.datetime对象.您可以按照上面的说明在更改对象时进行更改,或者在创建对象时执行以下操作:

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日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆