Skip to content

Instantly share code, notes, and snippets.

@thelahunginjeet
Created June 26, 2015 15:52
Show Gist options
  • Save thelahunginjeet/252369938a749671da58 to your computer and use it in GitHub Desktop.
Save thelahunginjeet/252369938a749671da58 to your computer and use it in GitHub Desktop.
Simple translator between 'mm/dd/yyyy hh:mm:ss' object and python datetime object.
import datetime as dt
def date_parser(datestring):
"""
Accepts a datestring in the format:
mm/dd/yyyy hh:mm:ss
and returns a python datetime object with all relevant fields properly set.
"""
mdy = [int(x) for x in datestring.split(' ')[0].split('/')]
hms = [int(x) for x in datestring.split(' ')[1].split(':')]
return dt.datetime(year=mdy[2],month=mdy[0],day=mdy[1],hour=hms[0],minute=hms[1],second=hms[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment