can1357/oh-my-pi · error · RangeError

Invalid time value

Error message

Invalid time value

What it means

asDate (packages/utils/src/dates.ts) copies or constructs a Date and throws RangeError("Invalid time value") when the resulting date's getTime() is NaN — i.e. the input was an invalid Date (e.g. new Date(NaN)) or an unparseable numeric timestamp. It guards the date-fns-compatible format() functions from producing "Invalid Date" strings.

Source

Thrown at packages/utils/src/dates.ts:94

			return pad(hours12);
		case "h":
			return String(hours12);
		case "mm":
			return pad(date.getMinutes());
		case "m":
			return String(date.getMinutes());
		case "ss":
			return pad(date.getSeconds());
		case "s":
			return String(date.getSeconds());
		case "a":
			return hours < 12 ? "AM" : "PM";
	}
}

function asDate(value: Date | number): Date {
	const date = value instanceof Date ? new Date(value.getTime()) : new Date(value);
	if (Number.isNaN(date.getTime())) throw new RangeError("Invalid time value");
	return date;
}

/** Format a date with the supported date-fns v4 tokens and quoted literals. */
export function format(value: Date | number, pattern: string): string {
	const date = asDate(value);
	let result = "";

	for (let index = 0; index < pattern.length; ) {
		if (pattern[index] === "'") {
			if (pattern[index + 1] === "'") {
				result += "'";
				index += 2;
				continue;
			}

			index++;
			while (index < pattern.length) {

View on GitHub (pinned to 9690622007)

Solutions

  1. Validate the Date before formatting: check Number.isNaN(date.getTime()) and handle the invalid case.
  2. Fix the source of the bad timestamp (malformed string, null, NaN) rather than formatting it.
  3. Use Date.parse() or a strict parsing routine on strings before constructing the Date.
  4. For numeric inputs, confirm the value is a finite epoch milliseconds number.

Example fix

// before
format(new Date(input), 'yyyy-MM-dd'); // RangeError when input is garbage
// after
const d = new Date(input);
if (Number.isNaN(d.getTime())) {
  return '—'; // or surface a validation error
}
format(d, 'yyyy-MM-dd');
Defensive patterns

Strategy: validation

Validate before calling

function isValidDate(v) {
  const d = v instanceof Date ? v : new Date(v);
  return !Number.isNaN(d.getTime());
}

Type guard

function isRealDate(value: Date | number): value is Date {
  const d = value instanceof Date ? value : new Date(value);
  return !Number.isNaN(d.getTime());
}

Try / catch

try {
  return format(value, 'yyyy-MM-dd HH:mm');
} catch (err) {
  if (err instanceof RangeError && err.message === 'Invalid time value') {
    return 'invalid date';
  }
  throw err;
}

Prevention

When it happens

Trigger: Calling format(), or any function routed through date()/asDate(), with a Date created from an invalid input (new Date('not-a-date')), NaN, Infinity, or a Date field deserialized as null then coerced.

Common situations: Formatting timestamps parsed from user input, JSON payloads where a date string was malformed, or clock values of 0/NaN from failed timers or DB reads.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/c3156781b2633dd4. Report an issue: GitHub.