Parse datetime strings in Python with dateutil
Did you know?
You can use
dateutil.parser
to parse all sorts of different date and datetime string formats.https://pypi.org/project/python-dateutil/
An example👇
from dateutil.parser import parse print(parse("2012-01-19 17:21:00")) # => 2012-01-19 17:21:00 print(parse("1st Jan, 2019")) # => 2019-01-01 00:00:00 print(parse("23.11.2020")) # => 2020-11-23 00:00:00
Unfortunately, this is quite slow:
from dateutil.parser import parse %timeit parse(datetime_txt).date() 54.3 µs ± 450 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each) import datetime %timeit datetime.datetime.strptime(datetime_txt, "%H:%M %d-%m-%Y").date() 7.44 µs ± 240 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)