Skip to content

Instantly share code, notes, and snippets.

@drboyer
Created March 12, 2015 18:47
Show Gist options
  • Save drboyer/3c9dd9f0e271e56dd0f0 to your computer and use it in GitHub Desktop.
Save drboyer/3c9dd9f0e271e56dd0f0 to your computer and use it in GitHub Desktop.
I always forget how to create a datetime object from a formatted string in Python 2.4 (or earlier), where the datetime.strptime method doesn't exist. You can easily make it using the time module though...
# Do imports of these modules somewhere up top
import time, datetime
# timestring is a string containing the time and/or date you want to parse
# format is the time format to parse.
# Reference: https://docs.python.org/2.6/library/datetime.html#strftime-and-strptime-behavior
mydatetime = datetime.datetime(*(time.strptime(timestring, format)[0:6]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment