0

I need to convert datetime to date without seconds in python, how to execute this:

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

Result:

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

I've tried everything
Can help?

rodrigosillos
  • 67
  • 2
  • 10
  • Possible duplicate of [How do I convert datetime to date (in Python)?](http://stackoverflow.com/questions/3743222/how-do-i-convert-datetime-to-date-in-python) – Bakri Bitar Jun 01 '16 at 22:30
  • *"I've tried everything"* - where *is* what you've tried, then? Broadly, either: 1. extract just the date part then parse it; or 2. parse the whole thing then call `.date()` to discard the time. – jonrsharpe Jun 01 '16 at 22:30
  • @BakriBitar The answers there just say to use `.date()`, which he's already doing. His problem is with the parsing. – Barmar Jun 01 '16 at 22:58
  • thank you so much! – rodrigosillos Jun 01 '16 at 23:06

1 Answers1

2

Even though you're going to discard the time, you still need to include it in your format string, since it exists in the string you're parsing. strptime() doesn't know that you don't care about the time, it just needs to match the input against the format string.

dt = datetime.strptime('2016-06-01 16:05:20', '%Y-%m-%d %H:%M:%S').date()
Barmar
  • 741,623
  • 53
  • 500
  • 612