def format_datetime(session):
    """Convert date or datetime object into formatted string representation.
    """
    if session.data is not None:
        date_format = session.field.opts.date_format
        if date_format == 'iso8601':
            session.data = session.data.isoformat()
        else:
            session.data = session.data.strftime(date_format)
    return session.data