n8n-io/n8n · error · ExpressionExtensionError

cannot convert to date

Error message

cannot convert to date

What it means

The STRING `.toDate()` uses `new Date(Date.parse(value))`. If the resulting Date's `.toString()` is `'Invalid Date'` (i.e. `Date.parse` returned NaN), it throws 'cannot convert to date'. This is the simpler sibling of 335 — it returns a JS `Date`, not a Luxon DateTime.

Source

Thrown at packages/@n8n/expression-runtime/src/extensions/string-extensions.ts:184

			.replace(/(`{3,})(.*?)\1/gm, '$2')
			.replace(/^-{3,}\s*$/g, '')
			.replace(/`(.+?)`/g, '$1')
			.replace(/\n{2,}/g, '\n\n');
	} catch (e) {
		return value;
	}
	return output;
}

function removeTags(value: string): string {
	return value.replace(/<[^>]*>?/gm, '');
}

function toDate(value: string): Date {
	const date = new Date(Date.parse(value));

	if (date.toString() === 'Invalid Date') {
		throw new ExpressionExtensionError('cannot convert to date');
	}
	// If time component is not specified, force 00:00h
	if (!/:/.test(value)) {
		date.setHours(0, 0, 0);
	}
	return date;
}

export function toDateTime(value: string, extraArgs: [string] = ['']): DateTime {
	try {
		const [valueFormat] = extraArgs;

		if (valueFormat) {
			if (
				valueFormat === 'ms' ||
				valueFormat === 's' ||
				valueFormat === 'us' ||
				valueFormat === 'excel'

View on GitHub (pinned to 5ac6606e81)

Solutions

  1. Pre-validate with `!isNaN(Date.parse(value))` in a Code node.
  2. If the format is fixed, parse with Luxon `DateTime.fromFormat` in a Code node and pass the Date forward.
  3. Filter or default empty values upstream so they never reach `.toDate()`.

Example fix

// before
{{ $json.when.toDate() }}
// after (guard upstream; only call .toDate() when Date.parse succeeds)
Defensive patterns

Strategy: try-catch

Validate before calling

const s = $json.when;
if (typeof s !== 'string' || isNaN(Date.parse(s))) {
  $json.__dateValid = false;
} else {
  $json.__dateValid = true;
}
return $json;

Type guard

const isParsableDate = (s: string): boolean => !isNaN(Date.parse(s));

Try / catch

try {
  return $json.when.toDate().toISOString();
} catch {
  return null;
}

Prevention

When it happens

Trigger: `.toDate()` on `'not-a-date'`, `''`, or any string `Date.parse` cannot interpret.

Common situations: Free-text or empty field where a date was expected; locale-specific format `Date.parse` does not recognise.

Related errors


AI-assisted analysis of n8n-io/n8n@5ac6606e81 (2026-08-12). Data as JSON: /api/errors/6c4bd36f94b53e1d. Report an issue: GitHub.