From d72f79ffb393a096f510f0c41bb66d987fe8cb0d Mon Sep 17 00:00:00 2001 From: Elias Meire Date: Thu, 31 Aug 2023 12:33:26 +0200 Subject: [PATCH] fix(Date & Time Node): Dont parse date if it's not set (null or undefined) (#7050) Github issue / Community forum post (link here to close automatically): https://community.n8n.io/t/date-time-node-transforming-null-to-19700101/29339 null/undefined input was converted to 01/01/1970 instead of being passed through as-is --- .../nodes/DateTime/V2/DateTimeV2.node.ts | 24 +++-- .../test/node/workflow.nullDate_v2.json | 93 +++++++++++++++++++ 2 files changed, 108 insertions(+), 9 deletions(-) create mode 100644 packages/nodes-base/nodes/DateTime/test/node/workflow.nullDate_v2.json diff --git a/packages/nodes-base/nodes/DateTime/V2/DateTimeV2.node.ts b/packages/nodes-base/nodes/DateTime/V2/DateTimeV2.node.ts index 0f680fe521..fd36d4623f 100644 --- a/packages/nodes-base/nodes/DateTime/V2/DateTimeV2.node.ts +++ b/packages/nodes-base/nodes/DateTime/V2/DateTimeV2.node.ts @@ -134,18 +134,24 @@ export class DateTimeV2 implements INodeType { const outputFieldName = this.getNodeParameter('outputFieldName', i) as string; const { timezone } = this.getNodeParameter('options', i) as { timezone: boolean }; - const dateLuxon = timezone - ? parseDate.call(this, date, workflowTimezone) - : parseDate.call(this, date); - if (format === 'custom') { - const customFormat = this.getNodeParameter('customFormat', i) as string; + if (date === null || date === undefined) { responseData.push({ - [outputFieldName]: dateLuxon.toFormat(customFormat), + [outputFieldName]: date, }); } else { - responseData.push({ - [outputFieldName]: dateLuxon.toFormat(format), - }); + const dateLuxon = timezone + ? parseDate.call(this, date, workflowTimezone) + : parseDate.call(this, date); + if (format === 'custom') { + const customFormat = this.getNodeParameter('customFormat', i) as string; + responseData.push({ + [outputFieldName]: dateLuxon.toFormat(customFormat), + }); + } else { + responseData.push({ + [outputFieldName]: dateLuxon.toFormat(format), + }); + } } } else if (operation === 'roundDate') { const date = this.getNodeParameter('date', i) as string; diff --git a/packages/nodes-base/nodes/DateTime/test/node/workflow.nullDate_v2.json b/packages/nodes-base/nodes/DateTime/test/node/workflow.nullDate_v2.json new file mode 100644 index 0000000000..a1c3a440ca --- /dev/null +++ b/packages/nodes-base/nodes/DateTime/test/node/workflow.nullDate_v2.json @@ -0,0 +1,93 @@ +{ + "name": "Null Date", + "nodes": [ + { + "parameters": {}, + "id": "62bb450c-f2e3-41c9-9894-a954d2c5e115", + "name": "When clicking \"Execute Workflow\"", + "type": "n8n-nodes-base.manualTrigger", + "typeVersion": 1, + "position": [ + 1600, + 740 + ] + }, + { + "parameters": { + "jsCode": "return {date: null}" + }, + "id": "47e4ce5b-2365-4987-a058-ae21964d135d", + "name": "Code", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [ + 1820, + 740 + ] + }, + { + "parameters": { + "operation": "formatDate", + "date": "={{ $json.date }}", + "options": {} + }, + "id": "9af21b91-9db6-40cd-98ee-ee969eb0a348", + "name": "Date & Time", + "type": "n8n-nodes-base.dateTime", + "typeVersion": 2, + "position": [ + 2040, + 740 + ] + } + ], + "pinData": { + "Date & Time": [ + { + "json": { + "formattedDate": null + } + } + ], + "Code": [ + { + "json": { + "date": null + } + } + ] + }, + "connections": { + "When clicking \"Execute Workflow\"": { + "main": [ + [ + { + "node": "Code", + "type": "main", + "index": 0 + } + ] + ] + }, + "Code": { + "main": [ + [ + { + "node": "Date & Time", + "type": "main", + "index": 0 + } + ] + ] + } + }, + "active": false, + "settings": { + "executionOrder": "v1" + }, + "versionId": "", + "meta": { + "instanceId": "78577815012af39cf16dad7a787b0898c42fb7514b8a7f99b2136862c2af502c" + }, + "tags": [] + }