Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Python 3 find this ISO8601 date: "2019-04-05T16:55:26Z" invalid?

Tags:

I supply "2019-04-05T16:55:26Z" to Python 3's datetime.datetime.fromisoformat and get Invalid isoformat string, though the same string works without the Z. ISO8601 allows for the Z - https://en.wikipedia.org/wiki/ISO_8601

$ python3
Python 3.7.2 (default, Feb 12 2019, 08:15:36)

>>> datetime.fromisoformat("2019-04-05T16:55:26Z")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Invalid isoformat string: '2019-04-05T16:55:26Z'

>>> datetime.fromisoformat("2019-04-05T16:55:26")
datetime.datetime(2019, 4, 5, 16, 55, 26)
like image 628
stephendwolff Avatar asked Apr 05 '19 19:04

stephendwolff


People also ask

What is ISO format of date in python?

In Python ISO 8601 date is represented in YYYY-MM-DDTHH:MM:SS. mmmmmm format. For example, May 18, 2022, is represented as 2022-05-18T11:40:22.519222.

How do I create an ISO date in python?

This string is converted into an ISO format string by using the . strftime() method. Here as we know that ISO format is YYYY-MM-DD so we convert it into this format by using the following format code- “%Y-%m-%dT%H:%M:%S. %f%z”.


1 Answers

I just checked the Python 3 documentation and it isn't intended to parse arbitrary ISO8601 format strings:

Caution: This does not support parsing arbitrary ISO 8601 strings - it is only intended as the inverse operation of datetime.isoformat().

like image 72
stephendwolff Avatar answered Sep 24 '22 16:09

stephendwolff