from datetime import datetime


def to_datetime(d):
    """Convert a variable to a `datetime` object.

    Checks various formats used in IMDb to convert the variable to a
    `datetime` object under those formats. The formats include:

    - `%d %B %Y`
    - `%Y`
    - `%Y-%m-%d`

    Args:
        d (:obj:`str`): A string to convert to a `datetime` object.
    
    Returns:
        :obj:`datetime`: A `datetime` object that was represented by the string, or `None` if `d` is `None`.
    
    Raises:
        :class:`ValueError`: If the string could not be converted.
    """

    if not d:
        return None
    try:
        return datetime.strptime(d, '%d %B %Y')
    except ValueError:
        try:
            return datetime.strptime(d, '%Y')
        except ValueError:
            try:
                return datetime.strptime(d, '%Y-%m-%d')
            except ValueError as e:
                raise e