{"record":{"id":"8ab510e99cb4e1b0","repo":"TheAlgorithms/JavaScript","slug":"invalid-day-value","errorCode":null,"errorMessage":"Invalid day value.","messagePattern":"Invalid day value\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Timing-Functions/ParseDate.js","lineNumber":5,"sourceCode":"import { getMonthDays } from './GetMonthDays'\n\nfunction checkDate(date) {\n  if (date.day < 1 || date.day > getMonthDays(date.month, date.year)) {\n    throw new Error('Invalid day value.')\n  }\n}\n\nfunction parseDate(dateString) {\n  const regex = /^(\\d{1,2})\\/(\\d{1,2})\\/(\\d{4})$/\n\n  const match = dateString.match(regex)\n\n  if (!match) {\n    throw new Error(\"Invalid date format. Please use 'dd/mm/yyyy'.\")\n  }\n\n  const res = {\n    day: parseInt(match[1], 10),\n    month: parseInt(match[2], 10),\n    year: parseInt(match[3], 10)\n  }\n  checkDate(res)","sourceCodeStart":1,"sourceCodeEnd":23,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Timing-Functions/ParseDate.js#L1-L23","documentation":"Thrown by checkDate() (called from parseDate) in Timing-Functions/ParseDate.js when date.day is less than 1 or greater than getMonthDays(date.month, date.year). It uses the GetMonthDays helper to bound the day to the real month length, so February in a leap year allows 29, otherwise 28. Generic Error indicating the day is out of range for the parsed month.","triggerScenarios":"Parsing a date whose day exceeds the month length: parseDate('31/04/2023') (April has 30 days) → day=31 > 30; parseDate('29/02/2023') (2023 not leap) → day=29 > 28; parseDate('00/01/2021') → day=0 < 1; parseDate('32/01/2021') → 32 > 31. Also note parseDate interprets match[1] as the DAY and match[2] as the MONTH (dd/mm/yyyy order).","commonSituations":"Mixing US (mm/dd/yyyy) and EU (dd/mm/yyyy) order — passing '02/30/2021' meaning Feb 30 in US order, but parseDate reads it as day=02, month=30 which throws Invalid Month Number first; allowing day=29 for February without checking the year; user-entered dates without a calendar widget.","solutions":["Confirm you are passing dd/mm/yyyy order, since parseDate reads the first group as the day.","Validate the day against the month length before calling, or rely on parseDate and catch the error.","Use a native Date object with proper year/month/day for arithmetic, since this helper is for parsing a fixed format.","For February 29, ensure the year is a leap year (isLeapYear) before accepting day=29."],"exampleFix":"// before\nparseDate('31/04/2023') // April has 30 days, throws\n\n// after\nparseDate('30/04/2023') // valid last day of April","handlingStrategy":"validation","validationCode":"function parseDateSafe(str) {\n  const d = parseDate(str) // parseDate already runs checkDate\n  return d\n}\n// or pre-check the day against the month length before calling","typeGuard":"const isPlausibleDayRange = (d, m, y) => d >= 1 && d <= getMonthDays(m, y)","tryCatchPattern":"try { parseDate('31/04/2023') } catch (e) { if (/Invalid day/.test(e.message)) { /* ask user to re-enter */ } else throw e }","preventionTips":["Remember parseDate expects dd/mm/yyyy order (day first).","Use a calendar widget to avoid impossible day/month combinations.","For February 29, ensure the year is a leap year before accepting."],"tags":["date","validation","business-rule","input-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}