from datetime import datetime


def valid_datetime(date_string, format):
    """Check if a string is a valid datetime."""
    try:
        return datetime.strptime(date_string, format)
    except ValueError:
        return None