jackwener/OpenCLI · error · ArgumentError

日期格式错误:${s},应为 YYYY-MM-DD

Error message

日期格式错误:${s},应为 YYYY-MM-DD

What it means

parseDate splits an input string on '-' and requires exactly three numeric parts (YYYY-MM-DD). If the shape is wrong or any part is NaN, it throws ArgumentError telling the user the expected format.

Source

Thrown at clis/mubu/notes.js:38

}

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)) {
    throw new ArgumentError(`月份格式错误:${s},应为 YYYY-MM`);
  }
  const [year, month] = parts;
  validateYear(year);
  validateMonth(month);
  return { year, month };
}

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Pass dates as YYYY-MM-DD, e.g. 2024-01-05.
  2. Normalize separators: s.replace(/\//g, '-') before parsing.
  3. Pre-validate the string with /^\d{4}-\d{2}-\d{2}$/.test(s).

Example fix

// before
parseDate('2024/01/05');
// after
parseDate('2024-01-05');
Defensive patterns

Strategy: validation

Validate before calling

const DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
if (!DATE_RE.test(s)) throw new Error(`date must be YYYY-MM-DD, got: ${s}`);

Type guard

const isDateString = (s) => typeof s === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(s);

Try / catch

try {
  const d = parseDate(input);
} catch (e) {
  if (/日期格式错误/.test(e.message)) console.error('Use YYYY-MM-DD format:', e.message);
  else throw e;
}

Prevention

When it happens

Trigger: Passing '2024/01/05', '2024-1', 'today', or an empty string to parseDate (via --from/--to flags or the d/start/end helpers).

Common situations: Users use slash-formatted dates, pass human words like 'yesterday', or shell quoting strips a hyphen.

Related errors


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