{"record":{"id":"6c4bd36f94b53e1d","repo":"n8n-io/n8n","slug":"cannot-convert-to-date","errorCode":null,"errorMessage":"cannot convert to date","messagePattern":"cannot convert to date","errorType":"exception","errorClass":"ExpressionExtensionError","httpStatus":null,"severity":"error","filePath":"packages/@n8n/expression-runtime/src/extensions/string-extensions.ts","lineNumber":184,"sourceCode":"\t\t\t.replace(/(`{3,})(.*?)\\1/gm, '$2')\n\t\t\t.replace(/^-{3,}\\s*$/g, '')\n\t\t\t.replace(/`(.+?)`/g, '$1')\n\t\t\t.replace(/\\n{2,}/g, '\\n\\n');\n\t} catch (e) {\n\t\treturn value;\n\t}\n\treturn output;\n}\n\nfunction removeTags(value: string): string {\n\treturn value.replace(/<[^>]*>?/gm, '');\n}\n\nfunction toDate(value: string): Date {\n\tconst date = new Date(Date.parse(value));\n\n\tif (date.toString() === 'Invalid Date') {\n\t\tthrow new ExpressionExtensionError('cannot convert to date');\n\t}\n\t// If time component is not specified, force 00:00h\n\tif (!/:/.test(value)) {\n\t\tdate.setHours(0, 0, 0);\n\t}\n\treturn date;\n}\n\nexport function toDateTime(value: string, extraArgs: [string] = ['']): DateTime {\n\ttry {\n\t\tconst [valueFormat] = extraArgs;\n\n\t\tif (valueFormat) {\n\t\t\tif (\n\t\t\t\tvalueFormat === 'ms' ||\n\t\t\t\tvalueFormat === 's' ||\n\t\t\t\tvalueFormat === 'us' ||\n\t\t\t\tvalueFormat === 'excel'","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/expression-runtime/src/extensions/string-extensions.ts#L166-L202","documentation":"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.","triggerScenarios":"`.toDate()` on `'not-a-date'`, `''`, or any string `Date.parse` cannot interpret.","commonSituations":"Free-text or empty field where a date was expected; locale-specific format `Date.parse` does not recognise.","solutions":["Pre-validate with `!isNaN(Date.parse(value))` in a Code node.","If the format is fixed, parse with Luxon `DateTime.fromFormat` in a Code node and pass the Date forward.","Filter or default empty values upstream so they never reach `.toDate()`."],"exampleFix":"// before\n{{ $json.when.toDate() }}\n// after (guard upstream; only call .toDate() when Date.parse succeeds)","handlingStrategy":"try-catch","validationCode":"const s = $json.when;\nif (typeof s !== 'string' || isNaN(Date.parse(s))) {\n  $json.__dateValid = false;\n} else {\n  $json.__dateValid = true;\n}\nreturn $json;","typeGuard":"const isParsableDate = (s: string): boolean => !isNaN(Date.parse(s));","tryCatchPattern":"try {\n  return $json.when.toDate().toISOString();\n} catch {\n  return null;\n}","preventionTips":["Pre-validate with `Date.parse` in a Code node.","Use Luxon `DateTime.fromFormat` for fixed non-standard formats.","Filter or default empty values upstream."],"tags":["expression","string","date","parsing","data-quality"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}