jackwener/OpenCLI · error · ArgumentError

日期非法:${year}-${month}-${day}(${year} 年 ${month} 月共 ${maxDay}

Error message

日期非法:${year}-${month}-${day}(${year} 年 ${month} 月共 ${maxDay} 天)

What it means

validateDay checks that the day is an integer between 1 and the last day of the given year/month (leap-year aware via lastDayOfMonth). The library throws ArgumentError when the day exceeds the month's length or is otherwise invalid, preventing silently shifted ranges.

Source

Thrown at clis/mubu/notes.js:31

  return new Date(year, month, 0).getDate();
}

function validateYear(year, label = '年份') {
  if (!Number.isInteger(year) || year < 1) {
    throw new ArgumentError(`${label} 非法:${year},应为正整数`);
  }
}

function validateMonth(month) {
  if (!Number.isInteger(month) || month < 1 || month > 12) {
    throw new ArgumentError(`月份非法:${month},应为 1-12`);
  }
}

function validateDay(year, month, day) {
  const maxDay = lastDayOfMonth(year, month);
  if (!Number.isInteger(day) || day < 1 || day > maxDay) {
    throw new ArgumentError(`日期非法:${year}-${month}-${day}(${year} 年 ${month} 月共 ${maxDay} 天)`);
  }
}

function parseDate(s) {
  const parts = s.split('-').map(Number);
  if (parts.length !== 3 || parts.some(isNaN)) {
    throw new ArgumentError(`日期格式错误:${s},应为 YYYY-MM-DD`);
  }
  const [year, month, day] = parts;
  validateYear(year);
  validateMonth(month);
  validateDay(year, month, day);
  return { year, month, day };
}

function parseMonth(s) {
  const parts = s.split('-').map(Number);
  if (parts.length !== 2 || parts.some(isNaN)) {

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Use a valid day for that month (2024-02-29 is OK, 2023-02-29 is not).
  2. Let a Date object clamp the day: new Date(year, month-1, day) then re-read getDate().
  3. Validate with a calendar check before calling parseDate.

Example fix

// before
const d = parseDate('2023-02-29');
// after
const d = parseDate('2024-02-29'); // leap year
Defensive patterns

Strategy: validation

Validate before calling

function isValidDay(y, m, d) {
  const max = new Date(y, m, 0).getDate();
  return Number.isInteger(d) && d >= 1 && d <= max;
}
if (!isValidDay(2024, 2, 29)) throw new Error('invalid day for 2024-02');

Type guard

const isValidDay = (y, m, d) => {
  const max = new Date(y, m, 0).getDate();
  return Number.isInteger(d) && d >= 1 && d <= max;
};

Try / catch

try {
  const d = parseDate(input);
} catch (e) {
  if (/日期非法/.test(e.message)) console.error('Day out of range for that month:', e.message);
  else throw e;
}

Prevention

When it happens

Trigger: parseDate('2023-02-29') (non-leap year), parseDate('2024-04-31') (April has 30 days), or day=0 / non-integer day.

Common situations: Users hand-write dates like 02-30, scripts compute day from arithmetic overflow, or assume every month has 31 days.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/cc8fa9b3327f570f. Report an issue: GitHub.