sansan0/TrendRadar · error · InvalidParameterError
日期格式错误: {date_str}
Error message
日期格式错误: {date_str} What it means
Raised by validate_date when datetime.strptime(date_str, "%Y-%m-%d") fails. Only the exact ISO form is accepted: 4-digit year, dash, 2-digit month, dash, 2-digit day. Common failures include slashes (2025/10/11), single-digit parts (2025-9-1), timestamps (2025-10-11T00:00), or invalid calendar dates (2025-02-30).
Source
Thrown at mcp_server/utils/validators.py:310
def validate_date(date_str: str) -> datetime:
"""
验证日期格式
Args:
date_str: 日期字符串 (YYYY-MM-DD)
Returns:
datetime对象
Raises:
InvalidParameterError: 日期格式错误
"""
try:
return datetime.strptime(date_str, "%Y-%m-%d")
except ValueError:
raise InvalidParameterError(
f"日期格式错误: {date_str}",
suggestion="请使用 YYYY-MM-DD 格式,例如: 2025-10-11"
)
def normalize_date_range(date_range: Optional[Union[dict, str]]) -> Optional[Union[dict, str]]:
"""
规范化 date_range 参数
某些 MCP 客户端(特别是 HTTP 方式)会将 JSON 对象序列化为字符串传入。
此函数尝试将 JSON 字符串解析为 dict,如果不是 JSON 格式则保持原样。
Args:
date_range: 日期范围,可能是:
- dict: {"start": "2025-01-01", "end": "2025-01-07"}
- JSON 字符串: '{"start": "2025-01-01", "end": "2025-01-07"}'
- 普通字符串: "今天", "昨天", "2025-01-01"
- NoneView on GitHub (pinned to 8ee26026ba)
Solutions
- Use exact YYYY-MM-DD with zero-padded parts: 2025-10-11
- Client-side, format with a library before sending: date.toISOString().slice(0, 10) (JS) or strftime('%Y-%m-%d') (Python)
- Strip time components before sending; the validator does not accept timestamps
- Wrap calls in try/except InvalidParameterError and re-format/retry once
Example fix
// before
{"date_range": {"start": "2025/10/01", "end": "2025-10-11"}}
// after
{"date_range": {"start": "2025-10-01", "end": "2025-10-11"}} Defensive patterns
Strategy: validation
Validate before calling
const iso = (d: Date) => d.toISOString().slice(0, 10);
if (!/^\d{4}-\d{2}-\d{2}$/.test(dateStr)) dateStr = iso(new Date(dateStr)); Type guard
const isIsoDate = (s: string): boolean => {
if (!/^\d{4}-\d{2}-\d{2}$/.test(s)) return false;
const d = new Date(s + 'T00:00:00Z');
return !Number.isNaN(d.getTime());
}; Try / catch
try { call({ date_range: dr }); }
catch (e) {
if (/日期格式错误/.test(e.message)) call({ date_range: { start: iso(new Date(dr.start)), end: iso(new Date(dr.end)) } });
else throw e;
} Prevention
- Always format dates through a date library to YYYY-MM-DD before sending
- Slice off time components: toISOString().slice(0, 10)
- Reject non-zero-padded user input client-side
When it happens
Trigger: Passing start/end inside date_range, or any date-typed parameter, in a non-ISO format: "2025.10.11", "11-10-2025", "2025-10-11 00:00:00", "2025-1-01". Zero-padded ISO only.
Common situations: Locale-specific date habits (US mm-dd-yyyy, dots in Europe); frontend date pickers emitting full ISO 8601 timestamps; LLM-generated dates without zero-padding; Excel-exported dates with slashes.
Related errors
- INVALID_PARAMETER
- 不能查询未来的日期: {date.strftime('%Y-%m-%d')}
- 日期太久远: {date.strftime('%Y-%m-%d')} ({days_ago}天前)
- 日期表达式不能为空
- 日期格式错误: {stripped}
AI-assisted analysis of sansan0/TrendRadar@8ee26026ba (2026-08-15).
Data as JSON: /api/errors/ed84f463d11139b5.
Report an issue: GitHub.