def json_date_formatter(obj):
    """
    Used to format datetimes when serialising JSON.

    Usage: json.dumps(x, default=json_date_formatter)

    :param obj: object to serialise
    :return: serialised object
    """
    if hasattr(obj, 'isoformat'):
        return obj.isoformat()
    else:
        raise TypeError
