import datetime
import time
def to_datetime(datetime_string, format='%Y-%m-%d %H:%M:%S'):
"""Turn a date/time string into a datetime object."""
return datetime.datetime.fromtimestamp(
time.mktime(time.strptime(datetime_string, format)))
def to_date(date_string, format='%Y-%m-%d'):
"""Turn a date string into a date object."""
return datetime.date.fromtimestamp(
time.mktime(time.strptime(date_string, format)))
# Or, as of Python 2.5:
from datetime import datetime
def to_datetime(datetime_string, format='%Y-%m-%d %H:%M:%S'):
"""Turn a date/time string into a datetime object."""
return datetime.strptime(datetime_string, format)
def to_date(date_string, format='%Y-%m-%d'):
"""Turn a date string into a date object."""
return to_datetime(date_string, format).date()