TheAlgorithms/JavaScript · error · Error
Invalid day value.
Error message
Invalid day value.
What it means
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.
Source
Thrown at Timing-Functions/ParseDate.js:5
import { getMonthDays } from './GetMonthDays'
function checkDate(date) {
if (date.day < 1 || date.day > getMonthDays(date.month, date.year)) {
throw new Error('Invalid day value.')
}
}
function parseDate(dateString) {
const regex = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/
const match = dateString.match(regex)
if (!match) {
throw new Error("Invalid date format. Please use 'dd/mm/yyyy'.")
}
const res = {
day: parseInt(match[1], 10),
month: parseInt(match[2], 10),
year: parseInt(match[3], 10)
}
checkDate(res)View on GitHub (pinned to 5c39e87a9a)
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.
Example fix
// before
parseDate('31/04/2023') // April has 30 days, throws
// after
parseDate('30/04/2023') // valid last day of April Defensive patterns
Strategy: validation
Validate before calling
function parseDateSafe(str) {
const d = parseDate(str) // parseDate already runs checkDate
return d
}
// or pre-check the day against the month length before calling Type guard
const isPlausibleDayRange = (d, m, y) => d >= 1 && d <= getMonthDays(m, y)
Try / catch
try { parseDate('31/04/2023') } catch (e) { if (/Invalid day/.test(e.message)) { /* ask user to re-enter */ } else throw e } Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- ${creditCardString} is an invalid credit card number because
- Invalid Month Number.
- Invalid date format. Please use 'dd/mm/yyyy'.
- Invalid Triangle sides.
- Input is not a valid 2D matrix.
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/8ab510e99cb4e1b0.
Report an issue: GitHub.