Skip to content

Instantly share code, notes, and snippets.

@aviafelix
Created April 27, 2015 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aviafelix/39f32c5728c80acc38bb to your computer and use it in GitHub Desktop.
Save aviafelix/39f32c5728c80acc38bb to your computer and use it in GitHub Desktop.
Python datetime / dateutil parsing dates example
import datetime
import dateutil.parser
timestring1 = '2007-03-04T21:08:12.127'
timestring2 = '2012-03-04 12:08:12.354'
dt1 = datetime.datetime.strptime(timestring1, '%Y-%m-%dT%H:%M:%S.%f')
dt2 = datetime.datetime.strptime(timestring2, '%Y-%m-%d %H:%M:%S.%f')
dup1 = dateutil.parser.parse(timestring1)
dup2 = dateutil.parser.parse(timestring2)
@aviafelix
Copy link
Author

An example format for strings with timezones: "%Y-%m-%dT%H:%M:%S.%f%z".
But anyway it's better to use dateutil.parser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment