snippets / convert iso8601 date into datetime object

Language: Python Interactive Console - First posted by benoitc on 2007-10-1 11:23 (1 year, 1 month)
Link to the snippet: http://www.friendsnippets.org/snippet/28/

Many file formats and standards use the ISO 8601 date format (e.g. 2007-01-14T20:34:22+00:00) to store dates in a neutral, unambiguous manner. Here is a way to convert them in datetime format

1 >>> from datetime import datetime
2 >>> from time import strptime
3 >>> s="2007-10-01T02:06:54Z"
4 >>> datetime(*strptime(s,"%Y-%m-%dT%H:%M:%SZ")[0:6])
5 datetime.datetime(2007, 10, 1, 2, 6, 54)
In order to post a comment, you should have a friendsnippet account. Please sign-in.

0 comments

Oct '07
  • Many file formats and standards use the ISO 8601 date format (e.g. 2007-01-14T20:34:22+00:00) to store dates in a neutral, unambiguous manner. Here is a way to convert them in datetime format