Created
October 21, 2016 22:58
-
-
Save dpenfoldbrown/cc4266739e00e7b413e631769d59087a to your computer and use it in GitHub Desktop.
Date extracting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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