Skip to content

Instantly share code, notes, and snippets.

@carlosescri
Created August 28, 2013 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlosescri/6363927 to your computer and use it in GitHub Desktop.
Save carlosescri/6363927 to your computer and use it in GitHub Desktop.
Create a datetime from a string in a given format.
import time
from datetime import datetime
def str_to_datetime(str_date, str_format="%Y-%m-%d %H:%M"):
""" Creates a datetime from a string in a given format. """
dt = time.strptime(str_date, str_format)
dt = time.mktime(dt)
return datetime.fromtimestamp(dt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment