简体   繁体   English

如何在python中将字符串转换为没有时间的日期

[英]how to convert string to date without time in python

I am converting a string to date like this: 我正在将字符串转换为日期,如下所示:

date=request.GET.get('date','')
    if date:
        date = datetime.strptime(date, "%m/%d/%Y")
        print date

This prints: 这打印:

2014-08-08 00:00:00 2014-08-08 00:00:00

How can I get the date without the 00:00:00 time? 如何在没有00:00:00时间的情况下获取日期?

You have a "datetime" object, hence the time part. 你有一个“日期时间”对象,因此是时间部分。

As @jonrsharpe mentioned in his comment, it's 正如@jonrsharpe在评论中提到的那样

print date.date() (output in ISO format) print date.date() (以ISO格式输出)

or print date.strftime('%d/%m/%Y') print date.strftime('%d/%m/%Y')

in your given input format. 以您给定的输入格式。

from datetime import date

today = date.today()
print today.strftime('%d/%m/%Y')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM