Created
October 14, 2018 03:26
-
-
Save technocrat/d02e2c703001a9a0ba54111bdcea87b6 to your computer and use it in GitHub Desktop.
Python utility program to convert mm/dd/yyyy to ISO yyyy-mm-dd
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
""" | |
function to convert dates into date objects | |
""" | |
from datetime import datetime | |
import re | |
def canonize_date(slashdate): | |
"""convert date strings from 3/1/2009 to 2009-03-01""" | |
dateString = re.compile(r'(\d{1,2})/(\d{1,2})/(\d{4})') # match 3/1/2009 | |
dash = '-' | |
parts = dateString.search(slashdate).groups() | |
composed = parts[-1] + dash + parts[-3] + dash + parts[-2] | |
return composed | |
def make_date(entry): | |
""" | |
Convert string in form 2011-10-25 to a date object | |
""" | |
return datetime.date(datetime.strptime(entry, "%Y-%m-%d")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment