How to parse str (my_datetime) using strptime?

This is the standard string representation in datetime:

>>> from datetime import datetime, timezone
>>> dt = datetime(2017, 1, 1, tzinfo=timezone.utc)
>>> print(dt)
2017-01-01 00:00:00+00:00

What is the correct format string for parsing with datetime.strptime? That is, what happens instead of '???' to see the following result:

>>> from dateutil.parser import parse
>>> parse(date_str) == datetime.strptime('???', date_str)
True
+4
source share
3 answers

Unable to use strptime, as explained here .

upgrade to Python 3.7 and use the method datetime.fromisoformat.

Paul Hansle in issue15873 .

0
source

, str(d) d.isoformat(' '). %Y-%m-%d %H:%M:%S (2017-01-01 00:00:00), :

  • .%f ;
  • , %z.

.strptime , format, , " t .

+3

, .

-2

Source: https://habr.com/ru/post/1676413/


All Articles