jackwener/OpenCLI · error · Error

无效的时间格式: "${input}"

Error message

无效的时间格式: "${input}"

What it means

toUnixSeconds converts a time input (Date object, numeric timestamp string, or date string) into Unix seconds. It throws this error when the input is neither a Date, an all-digit numeric string, nor a string parseable by `new Date()` — i.e. the parsed milliseconds are NaN.

Source

Thrown at clis/douyin/_shared/timing.js:20

const MAX_OFFSET = 14 * 86400; // 14 days
export function validateTiming(unixSeconds) {
    if (!Number.isFinite(unixSeconds))
        throw new Error(`无效的时间戳: ${unixSeconds}`);
    const now = Math.floor(Date.now() / 1000);
    if (unixSeconds < now + MIN_OFFSET)
        throw new Error(`定时发布时间必须在至少 2 小时后`);
    if (unixSeconds > now + MAX_OFFSET)
        throw new Error(`定时发布时间不能超过 14 天`);
}
export function toUnixSeconds(input) {
    if (typeof input === 'number')
        return input;
    if (/^\d+$/.test(input)) {
        return Number(input);
    }
    const ms = new Date(input).getTime();
    if (isNaN(ms))
        throw new Error(`无效的时间格式: "${input}"`);
    return Math.floor(ms / 1000);
}

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Print and correct the input string to a format `new Date()` accepts (ISO 8601 like 2024-01-15T10:30:00Z)
  2. Pass a numeric epoch string (pure digits) or a Date object instead of a freeform string
  3. Ensure env/config values are non-empty; trim whitespace before calling
  4. Pre-parse manually, e.g. dayjs(input).unix(), for locale-specific formats

Example fix

// before
const ts = toUnixSeconds(process.env.PUBLISH_AT); // "" -> throws
// after
const raw = (process.env.PUBLISH_AT || '').trim();
if (!raw) throw new Error('PUBLISH_AT is required');
const ts = toUnixSeconds(raw);
Defensive patterns

Strategy: validation

Validate before calling

function isParsableDate(input) {
  if (input instanceof Date) return !isNaN(input.getTime());
  if (typeof input === 'number') return Number.isFinite(input);
  if (typeof input === 'string' && /^\d+$/.test(input.trim())) return true;
  return typeof input === 'string' && input.trim() !== '' && !isNaN(new Date(input).getTime());
}

Type guard

function isValidTimeInput(input) {
  return input instanceof Date ||
    (typeof input === 'string' && (/^\d+$/.test(input) || !isNaN(new Date(input).getTime()))) ||
    typeof input === 'number';
}

Try / catch

let ts;
try { ts = toUnixSeconds(input); }
catch (e) { console.error(`bad time input: ${input}`); ts = Math.floor(Date.now() / 1000); }

Prevention

When it happens

Trigger: Calling toUnixSeconds (directly or via timing helpers in clis/douyin/_shared/timing.js) with a non-date string such as "", "yesterday", "2024/13/45", "15:30" (time-only in some engines returns Invalid Date), or a malformed config value.

Common situations: Reading a publish-time or schedule value from config/env that was mistyped; passing a localized date string the Node version can't parse; passing an empty string from an unset environment variable.

Related errors


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