How do I translate an ISO 8601 datetime string into a Python datetime object?

Andrey Fedorov

I'm getting a datetime string in a format like "2009-05-28T16:15:00" (this is ISO 8601, I believe). One hackish option seems to be to parse the string using time.strptime and passing the first six elements of the tuple into the datetime constructor, like:

datetime.datetime(*time.strptime("2007-03-04T21:08:12", "%Y-%m-%dT%H:%M:%S")[:6])

I haven't been able to find a "cleaner" way of doing this. Is there one?

Wes Winham

I prefer using the dateutil library for timezone handling and generally solid date parsing. If you were to get an ISO 8601 string like: 2010-05-08T23:41:54.000Z you'd have a fun time parsing that with strptime, especially if you didn't know up front whether or not the timezone was included. pyiso8601 has a couple of issues (check their tracker) that I ran into during my usage and it hasn't been updated in a few years. dateutil, by contrast, has been active and worked for me:

import dateutil.parser
yourdate = dateutil.parser.parse(datestring)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I convert an ISO 8601 Datetime to a string?

From Dev

How do I convert dates into ISO-8601 DateTime format in a Pandas dataframe

From Dev

Convert DateTime to ISO 8601

From Dev

ISO 8601 datetime now

From Dev

Convert DateTime to ISO 8601

From Dev

ISO 8601 Field to Python DateTime Field

From Dev

ISO 8601 / Kibana datetime conversion

From Dev

Convert DateTime to ISO 8601 DateTime Format with timezone

From Dev

String of ISO-8601 datetime to number of seconds in Java

From Dev

ISO8601 DateTime String to be displayed in local users timezone

From Dev

How do I construct a UTC `datetime` object in Python?

From Dev

Convert a datetime.timedelta into ISO 8601 duration in Python?

From Dev

How to write all DateTime fields as ISO 8601 strings?

From Dev

How to determinate "ISO 8601 month part" of datetime in SQL Server?

From Dev

How Do I use the formats in DateTime::Locale with a DateTime object?

From Dev

How do I add milliseconds to a DateTime object?

From Dev

How to convert string into datetime object in python?

From Dev

How do i convert this date string to datetime?

From Dev

Python datetime formatted string to datetime object

From Dev

Should ISO 8601 datetime for xCBL include dashes or not?

From Dev

DateTime ISO 8601 without timezone component

From Dev

Parse ISO 8601 to C# DateTime

From Dev

SQLite datetime comparison with 'now' in ISO8601

From Dev

Ruby check if datetime is a iso8601 and saving

From Dev

Parse YouTube ISO 8601 to DateTime C#

From Dev

Python - given a timezone-aware datetime object how do I get a timezone-naive UTC datetime object?

From Dev

Joda: how to generate an ISO datetime string with "Z" at the and

From Java

How do I parse an ISO-8601 formatted string that contains no punctuation in Java 8?

From Dev

How to truncate python datetime(ISO) to hour?

Related Related

  1. 1

    How do I convert an ISO 8601 Datetime to a string?

  2. 2

    How do I convert dates into ISO-8601 DateTime format in a Pandas dataframe

  3. 3

    Convert DateTime to ISO 8601

  4. 4

    ISO 8601 datetime now

  5. 5

    Convert DateTime to ISO 8601

  6. 6

    ISO 8601 Field to Python DateTime Field

  7. 7

    ISO 8601 / Kibana datetime conversion

  8. 8

    Convert DateTime to ISO 8601 DateTime Format with timezone

  9. 9

    String of ISO-8601 datetime to number of seconds in Java

  10. 10

    ISO8601 DateTime String to be displayed in local users timezone

  11. 11

    How do I construct a UTC `datetime` object in Python?

  12. 12

    Convert a datetime.timedelta into ISO 8601 duration in Python?

  13. 13

    How to write all DateTime fields as ISO 8601 strings?

  14. 14

    How to determinate "ISO 8601 month part" of datetime in SQL Server?

  15. 15

    How Do I use the formats in DateTime::Locale with a DateTime object?

  16. 16

    How do I add milliseconds to a DateTime object?

  17. 17

    How to convert string into datetime object in python?

  18. 18

    How do i convert this date string to datetime?

  19. 19

    Python datetime formatted string to datetime object

  20. 20

    Should ISO 8601 datetime for xCBL include dashes or not?

  21. 21

    DateTime ISO 8601 without timezone component

  22. 22

    Parse ISO 8601 to C# DateTime

  23. 23

    SQLite datetime comparison with 'now' in ISO8601

  24. 24

    Ruby check if datetime is a iso8601 and saving

  25. 25

    Parse YouTube ISO 8601 to DateTime C#

  26. 26

    Python - given a timezone-aware datetime object how do I get a timezone-naive UTC datetime object?

  27. 27

    Joda: how to generate an ISO datetime string with "Z" at the and

  28. 28

    How do I parse an ISO-8601 formatted string that contains no punctuation in Java 8?

  29. 29

    How to truncate python datetime(ISO) to hour?

HotTag

Archive