So in Python 3, you can generate an ISO 8601 date with .isoformat(), but you can"t convert a string created by isoformat() back into a datetime object because Python"s own datetime directives don"t match properly. That is, %z = 0500 instead of 05:00 (which is produced by .isoformat()).
>>> strDate = d.isoformat() >>> strDate "2015-02-04T20:55:08.914461+00:00" >>> objDate = datetime.strptime(strDate,"%Y-%m-%dT%H:%M:%S.%f%z") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:Python34Lib\_strptime.py", line 500, in _strptime_datetime tt, fraction = _strptime(data_string, format) File "C:Python34Lib\_strptime.py", line 337, in _strptime (data_string, format)) ValueError: time data "2015-02-04T20:55:08.914461+00:00" does not match format "%Y-%m-%dT%H:%M:%S.%f%z"
From Python"s strptime documentation: (https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior)
%z UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive). (empty), +0000, -0400, +1030
So, in short, Python does not even adhere to its own string formatting directives.
I know datetime is already terrible in Python, but this really goes beyond unreasonable into the land of plain stupidity.
Tell me this isn"t true.
As of Python 3.7 there is a method
datetime.fromisoformat() which is exactly the reverse for
If you have older Python, then this is the current best "solution" to this question:
pip install python-dateutil
import datetime import dateutil def getDateTimeFromISO8601String(s): d = dateutil.parser.parse(s) return d
It would be easy for me to develop native apps using Java, C++ or Objective-C and I am also able to learn Kotlin, Dart or Swift, but things are much easier when you just use Python. I have done a Djan...
The rate at which we produce data is growing steadily, thus creating even larger streams of continuously evolving data. Online news, micro-blogs, search queries are just a few examples of these contin...
Python: - The Bible - 3 Manuscripts in 1 book:
Grokking Deep Learning teaches you to build deep learning neural networks from scratch! In his immersive style, deep learning expert Andrew Trask shows you the hidden science so you can uncover every ...