How do I convert a date string to the YYYY-MM-DD format?

There is a special calendar module for tkinter called tkcalendar. When taking data from such a calendar, I get a string like DD. MM.YYYY, but for a database on SQLite, I need a string like YYYY-MM-DD. Unfortunately, the dateutil.parser module does not always translate the date string correctly. For example: I take the date for today, then I get: 2020-03-18 00:00:00, but if I take another date(08.04.2004), then the output is a hash: 2004-08-04 00:00:00. For some reason, the parser incorrectly translates DD. MM. YYYY to YYYY-MM-DD and sometimes changes the month and day in places. Please tell me a more accurate date parser that will not allow such errors.

Author: DGDays, 2020-03-18

1 answers

Use the standard datetime module to parse the string and convert it:

import datetime as DT

dt = DT.datetime.strptime('10.11.2019', '%d.%m.%Y')
print(dt)
# 2019-11-10 00:00:00

print(dt.strftime('%Y-%m-%d'))
# 2019-11-10
 3
Author: gil9red, 2020-03-18 11:41:48