我需要在 python 中将日期时间转换为没有秒数的日期

I need to convert datetime to date without seconds in python

我需要在 python 中将日期时间转换为没有秒的日期,如何执行:

from datetime import datetime
dt = datetime.strptime('2016-06-01 16:05:20', '%Y-%m-%d').date()
print dt

结果:

Traceback (most recent call last):
  File "teste.py", line 5, in <module>
    dt = datetime.strptime('2016-06-01 16:05:20', '%Y-%m-%d').date()
  File "/usr/lib/python2.7/_strptime.py", line 328, in _strptime
    data_string[found.end():])
ValueError: unconverted data remains:  16:05:20

我什么都试过了
可以帮忙吗?

即使您要丢弃时间,您仍然需要将它包含在您的格式字符串中,因为它存在于您正在解析的字符串中。 strptime()不知道你不关心时间,它只是需要将输入与格式字符串匹配。

dt = datetime.strptime('2016-06-01 16:05:20', '%Y-%m-%d %H:%M:%S').date()