from datetime import datetime


def extract_datetime(dictionary, key, mask):
    """ Extract a date time from a dictionary by it's key and if the value
    isn't a datetime will convert using a giving mask.

    :param dict dictionary:
    :param str|int key:
    :param str mask:
    :return: The date time from a dict by it's key
    :rtype: datetime
    """
    if dictionary.get(key) is None:
        return None
    if isinstance(dictionary.get(key), datetime):
        return dictionary.get(key)
    else:
        return datetime.strptime(dictionary.get(key), mask)