Skip to content

Instantly share code, notes, and snippets.

@technocrat
Created October 14, 2018 03:26
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 technocrat/d02e2c703001a9a0ba54111bdcea87b6 to your computer and use it in GitHub Desktop.
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
"""
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