I am developing a web application with Flask Python. I have a Mysql table with a text column date:
-- date -- 10/06/2020 18:50:17 10/06/2020 18:55:10 28/05/2020 22:18:06 29/03/2020 20:47:01 29/03/2020 21:29:14
These data above are date in string format. I need to convert these string dates into format dates.
I did this in python :
actions = Action.query.all() # This is SQLAlchemy request to get all the rows of table 'Action' for action in actions: date=action.date # convert this date string in date format date_time_obj = datetime.strptime(date, '%d/%m/%y %H:%M:%S') print(date_time_obj)
But I get this error output:
Traceback (most recent call last): File "j:/Dropbox/cff/Python/folder/test.py", line 18, in <module> date_time_obj = datetime.strptime(date, '%d/%m/%y %H:%M:%S') File "C:UsersNinoAppDataLocalProgramsPythonPython37lib_strptime.py", line 577, in _strptime_datetime tt, fraction, gmtoff_fraction = _strptime(data_string, format) File "C:UsersNinoAppDataLocalProgramsPythonPython37lib_strptime.py", line 359, in _strptime (data_string, format)) ValueError: time data '18/02/2020 20:14:31' does not match format '%d/%m/%y %H:%M:%S'
I don’t understand as ’18/02/2020 20:14:31′ corresponding perfectly to format ‘%d/%m/%y %H:%M:%S’
What is wrong in my code? Did I miss something?
Advertisement
Answer
You date format should be
from datetime import datetime datetime.strptime(date, '%d/%m/%Y %H:%M:%S')
(with Y in upper case)