def str_to_datetime(ts):
    """Format a string to a datetime object.

    This functions supports several date formats like YYYY-MM-DD, MM-DD-YYYY
    and YY-MM-DD. When the given data is None or an empty string, the function
    returns None.

    :param ts: string to convert

    :returns: a datetime object

    :raises IvalidDateError: when the given string cannot be converted into
        a valid date
    """
    if not ts:
        return None

    try:
        return dateutil.parser.parse(ts).replace(tzinfo=None)
    except Exception:
        raise InvalidDateError(date=str(ts))
Format a string to a datetime object.

    This functions supports several date formats like YYYY-MM-DD, MM-DD-YYYY
    and YY-MM-DD. When the given data is None or an empty string, the function
    returns None.

    :param ts: string to convert

    :returns: a datetime object

    :raises IvalidDateError: when the given string cannot be converted into
        a valid date