If your string is in ISO8601 format and you have Python 3.7+ you can use the following simple code:
import datetime.dateaDate = datetime.date.fromisoformat('2020-10-04')for dates and
import datetime.datetimeaDateTime = datetime.datetime.fromisoformat('2020-10-04 22:47:00')for strings containing date and time. If timestamps are included the function datetime.datetime.isoformat() supports the following format
YYYY-MM-DD[*HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]]]where * matches any single character. See also here and here