{"record":{"id":"81aab950610f9526","repo":"TheAlgorithms/JavaScript","slug":"invalid-month-number","errorCode":null,"errorMessage":"Invalid Month Number.","messagePattern":"Invalid Month Number\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Timing-Functions/GetMonthDays.js","lineNumber":20,"sourceCode":"  function that takes month number and its year and returns the number of days within it\n  * @param monthNumber.\n  * @param year.\n  e.g.: mahfoudh.arous@gmail.com -> true\n  e.g.: mahfoudh.arous.com ->false\n*/\n\nimport { isLeapYear } from '../Maths/LeapYear'\n\nconst getMonthDays = (monthNumber, year) => {\n  const the31DaysMonths = [1, 3, 5, 7, 8, 10, 12]\n  const the30DaysMonths = [4, 6, 9, 11]\n\n  if (\n    !the31DaysMonths.includes(monthNumber) &&\n    !the30DaysMonths.includes(monthNumber) &&\n    monthNumber !== 2\n  ) {\n    throw new TypeError('Invalid Month Number.')\n  }\n\n  if (the31DaysMonths.includes(monthNumber)) {\n    return 31\n  }\n\n  if (the30DaysMonths.includes(monthNumber)) {\n    return 30\n  }\n\n  if (isLeapYear(year)) {\n    return 29\n  }\n\n  return 28\n}\n\nexport { getMonthDays }","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Timing-Functions/GetMonthDays.js#L2-L38","documentation":"Thrown by getMonthDays() in Timing-Functions/GetMonthDays.js when monthNumber is not in the 31-day set [1,3,5,7,8,10,12], the 30-day set [4,6,9,11], and is not exactly 2 (February). A TypeError rejecting months like 0, 13, -1, or non-integer values that are not literally 2. The check uses strict includes, so 2.0 (which === 2) is accepted but 2.5 is rejected.","triggerScenarios":"Calling getMonthDays(0) (zero-based month from Date.getMonth()), getMonthDays(13), getMonthDays(-1), getMonthDays(2.5), getMonthDays('1') (string '1' is not === 1, rejected), getMonthDays(null) (not in sets, not === 2). getMonthDays(2) is accepted and routed to the leap-year branch.","commonSituations":"Forgetting that JavaScript Date.getMonth() returns 0–11 and passing its output directly; passing a month parsed from a string (parseInt is fine, but unconverted strings fail); off-by-one errors from UI dropdowns using 0-based indexing; negative or NaN values from failed parseInt.","solutions":["If your source is 0-based (Date.getMonth()), add 1: getMonthDays(date.getMonth() + 1, year).","Ensure the month is an integer in 1..12 before calling: const m = Number(month); if (m >= 1 && m <= 12) getMonthDays(m, year).","Validate with Number.isInteger(monthNumber) && monthNumber >= 1 && monthNumber <= 12.","Check that you are not passing a string month from form data — parseInt(it, 10) first."],"exampleFix":"// before\nconst days = getMonthDays(jsDate.getMonth(), jsDate.getFullYear()) // 0..11\n\n// after\nconst days = getMonthDays(jsDate.getMonth() + 1, jsDate.getFullYear())","handlingStrategy":"validation","validationCode":"function getMonthDaysSafe(month, year) {\n  const m = Number(month)\n  if (!Number.isInteger(m) || m < 1 || m > 12) {\n    throw new TypeError('Month must be an integer 1..12')\n  }\n  return getMonthDays(m, year)\n}","typeGuard":"const isValidMonth = (m) => Number.isInteger(m) && m >= 1 && m <= 12","tryCatchPattern":"try { getMonthDays(month, year) } catch (e) { if (/Invalid Month/.test(e.message)) { /* prompt user, fix off-by-one */ } else throw e }","preventionTips":["Remember Date.getMonth() is 0-based; always add 1 before passing.","Coerce string months with parseInt(s, 10) and validate the range.","Use Number.isInteger to reject fractional or NaN months early."],"tags":["date","validation","off-by-one","type-check","input-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}