Home python How to translate a string with a date in the format yyyy-MM-DD?

How to translate a string with a date in the format yyyy-MM-DD?

Author

Date

Category

There is a special calendar module for tkinter called TKCalendar. When taking data from such a calendar, I get a string of the type dd.mm.yyyy, but for the database on SQLite I need a string of the kind of yyyy-mm-dd. Unfortunately, the dateutil.parser module does not always translate the line with the date right. For example: I take the date for today, I get: 2020-03-18 00:00:00 , but if I take another date (08.04.2004 ), The output is moving: 2004-08-04 00:00:00 . Parser for some reason incorrectly translates dd.mm.yyyy in yyyy-mm-dd and sometimes changes month and day in some places. Tell me, please, more accurate parser dates that will not allow such errors.


Answer 1, Authority 100%

Use the standard DateTime module for string and conversion parsing:

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

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions