Skip to content

Instantly share code, notes, and snippets.

@dpenfoldbrown
Created October 21, 2016 22:58
Show Gist options
  • Save dpenfoldbrown/cc4266739e00e7b413e631769d59087a to your computer and use it in GitHub Desktop.
Save dpenfoldbrown/cc4266739e00e7b413e631769d59087a to your computer and use it in GitHub Desktop.
Date extracting
def date_to_datetime(date_str, fstrs=None):
"""
Given date string, return datetime object
2015 format: 2/8/13 0:00
2016 format: 02/08/2013 00:00
02/12/2013 20:05
"""
if fstrs is None:
fstrs = ["%m/%d/%y %H:%M", "%m/%d/%Y %H:%M"]
for f in fstrs:
try:
return datetime.strptime(date_str, f)
except ValueError:
pass
raise ValueError("Date {0} matched no formats in list: {1}".format(
date_str, fstrs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment