{"record":{"id":"0ef2216786f621db","repo":"TheAlgorithms/Python","slug":"month-must-be-between-1-12","errorCode":null,"errorMessage":"Month must be between 1 - 12","messagePattern":"Month must be between 1 - 12","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/zellers_congruence.py","lineNumber":95,"sourceCode":"        \"1\": \"Monday\",\n        \"2\": \"Tuesday\",\n        \"3\": \"Wednesday\",\n        \"4\": \"Thursday\",\n        \"5\": \"Friday\",\n        \"6\": \"Saturday\",\n    }\n\n    convert_datetime_days = {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 0}\n\n    # Validate\n    if not 0 < len(date_input) < 11:\n        raise ValueError(\"Must be 10 characters long\")\n\n    # Get month\n    m: int = int(date_input[0] + date_input[1])\n    # Validate\n    if not 0 < m < 13:\n        raise ValueError(\"Month must be between 1 - 12\")\n\n    sep_1: str = date_input[2]\n    # Validate\n    if sep_1 not in [\"-\", \"/\"]:\n        raise ValueError(\"Date separator must be '-' or '/'\")\n\n    # Get day\n    d: int = int(date_input[3] + date_input[4])\n    # Validate\n    if not 0 < d < 32:\n        raise ValueError(\"Date must be between 1 - 31\")\n\n    # Get second separator\n    sep_2: str = date_input[5]\n    # Validate\n    if sep_2 not in [\"-\", \"/\"]:\n        raise ValueError(\"Date separator must be '-' or '/'\")\n","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/zellers_congruence.py#L77-L113","documentation":"Raised by zeller(date_input) in maths/zellers_congruence.py when the two-digit month parsed from date_input[0:2] is not in 1..12. The function slices characters positionally rather than tokenizing, so positions 0-1 are always interpreted as the month. A non-numeric month like '.2' instead raises 'invalid literal for int()' from int() before this check can run.","triggerScenarios":"Calling zeller('13-31-2010') (month 13); passing a date in DD-MM-YYYY order where the day exceeds 12, e.g. zeller('31-01-2010'); single-digit month written as '1-31-2010' shifts all positions and can produce a bogus month.","commonSituations":"Day/month transposition between US (MM-DD) and European (DD-MM) conventions; zero-padding omission ('1-31-2010' is 9 characters and also breaks positions); unvalidated free-text date fields.","solutions":["Convert your date to MM-DD-YYYY with strftime('%m-%d-%Y') which guarantees a valid, zero-padded month.","If accepting user input, validate with datetime.strptime(s, '%m-%d-%Y') first so month range is enforced with a clear error.","Document/require the MM-DD-YYYY order at every entry point that feeds this function."],"exampleFix":"# before\nzeller(f'{day:02d}-{month:02d}-{year}')  # DD-MM order -> month 31\n\n# after\nzeller(f'{month:02d}-{day:02d}-{year}')  # MM-DD-YYYY","handlingStrategy":"validation","validationCode":"from datetime import datetime\n\ndef normalize_to_mmddyyyy(s: str) -> str:\n    return datetime.strptime(s.strip(), '%m-%d-%Y').strftime('%m-%d-%Y')","typeGuard":null,"tryCatchPattern":"try:\n    day = zeller(s)\nexcept ValueError as e:\n    if 'Month' in str(e):\n        s = swap_day_month(s)  # only if DD-MM input is expected\n        day = zeller(s)\n    else:\n        raise","preventionTips":["Pin the MM-DD order at every input boundary; document it.","strptime with %m-%d-%Y enforces month range with a clear error."],"tags":["date","validation","string-parsing","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}