How to convert datetime to and from ISO 8601 string

ISO 8601 is a standardized format for representing date and time that is popular. Python has built-in support to convert to and from this format. But confusingly, those methods are distributed across two different modules!

  • Convert a datetime object to string in ISO 8601 format:
import datetime
datetime_str = some_datetime_obj.isoformat()
  • Convert a ISO 8601 format string to datetime object:
import dateutil.parser
some_datetime_obj = dateutil.parser.parse(datetime_str)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s