From 77106520c8942c746bb1ddffcddcde68a7059805 Mon Sep 17 00:00:00 2001 From: Michael Kret <88898367+michael-radency@users.noreply.github.com> Date: Wed, 5 Apr 2023 13:05:51 +0300 Subject: [PATCH] fix(Date & Time Node): Convert luxon DateTime object to ISO --- .../nodes/DateTime/DateTime.node.ts | 10 +- .../test/node/workflow.luxonDateTime.json | 165 ++++++++++++++++++ 2 files changed, 174 insertions(+), 1 deletion(-) create mode 100644 packages/nodes-base/nodes/DateTime/test/node/workflow.luxonDateTime.json diff --git a/packages/nodes-base/nodes/DateTime/DateTime.node.ts b/packages/nodes-base/nodes/DateTime/DateTime.node.ts index 26facc9e17..31db6988a2 100644 --- a/packages/nodes-base/nodes/DateTime/DateTime.node.ts +++ b/packages/nodes-base/nodes/DateTime/DateTime.node.ts @@ -1,4 +1,5 @@ import type { + IDataObject, IExecuteFunctions, ILoadOptionsFunctions, INodeExecutionData, @@ -6,12 +7,15 @@ import type { INodeType, INodeTypeDescription, } from 'n8n-workflow'; + import { deepCopy, NodeOperationError } from 'n8n-workflow'; import set from 'lodash.set'; import moment from 'moment-timezone'; +import { DateTime as LuxonDateTime } from 'luxon'; + function parseDateByFormat(this: IExecuteFunctions, value: string, fromFormat: string) { const date = moment(value, fromFormat, true); if (moment(date).isValid()) return date; @@ -411,12 +415,16 @@ export class DateTime implements INodeType { item = items[i]; if (action === 'format') { - const currentDate = this.getNodeParameter('value', i) as string; + let currentDate = this.getNodeParameter('value', i) as string; const dataPropertyName = this.getNodeParameter('dataPropertyName', i); const toFormat = this.getNodeParameter('toFormat', i) as string; const options = this.getNodeParameter('options', i); let newDate; + if ((currentDate as unknown as IDataObject) instanceof LuxonDateTime) { + currentDate = (currentDate as unknown as LuxonDateTime).toISO(); + } + if (currentDate === undefined) { continue; } diff --git a/packages/nodes-base/nodes/DateTime/test/node/workflow.luxonDateTime.json b/packages/nodes-base/nodes/DateTime/test/node/workflow.luxonDateTime.json new file mode 100644 index 0000000000..543be9d212 --- /dev/null +++ b/packages/nodes-base/nodes/DateTime/test/node/workflow.luxonDateTime.json @@ -0,0 +1,165 @@ +{ + "name": "My workflow 10", + "nodes": [ + { + "parameters": {}, + "id": "4d3b2f7b-1ae9-432d-b765-c6134cd11afc", + "name": "When clicking \"Execute Workflow\"", + "type": "n8n-nodes-base.manualTrigger", + "typeVersion": 1, + "position": [-60, 2060] + }, + { + "parameters": { + "value": "={{ DateTime.fromISO($json.testData) }}", + "toFormat": "X", + "options": { + "toTimezone": "UTC" + } + }, + "id": "31ad53db-5cfd-44e5-917b-b90105ae687d", + "name": "Date & Time", + "type": "n8n-nodes-base.dateTime", + "typeVersion": 1, + "position": [400, 1960] + }, + { + "parameters": { + "value": "={{ DateTime.fromISO($json.testData).minus({days:1}) }}", + "toFormat": "X", + "options": { + "toTimezone": "UTC" + } + }, + "id": "f959c146-2cd3-4bc5-aa55-5b974466ba90", + "name": "Date & Time1", + "type": "n8n-nodes-base.dateTime", + "typeVersion": 1, + "position": [400, 2180] + }, + { + "parameters": { + "value": "={{ Number($json.data) }}", + "toFormat": "YYYY-MM-DD", + "options": { + "toTimezone": "UTC" + } + }, + "id": "8bf5b635-607e-4207-8b0e-66f0540b103c", + "name": "Date & Time2", + "type": "n8n-nodes-base.dateTime", + "typeVersion": 1, + "position": [620, 1960] + }, + { + "parameters": { + "value": "={{ Number($json.data) }}", + "toFormat": "YYYY-MM-DD", + "options": { + "toTimezone": "UTC" + } + }, + "id": "350ce053-d415-4a7d-8447-6d7c73758f35", + "name": "Date & Time3", + "type": "n8n-nodes-base.dateTime", + "typeVersion": 1, + "position": [620, 2180] + }, + { + "parameters": { + "keepOnlySet": true, + "values": { + "string": [ + { + "name": "testData", + "value": "2023-03-31T06:31:01.000" + } + ] + }, + "options": {} + }, + "id": "b57950a6-3a6a-4e23-8c3b-16abe12bfe16", + "name": "Set", + "type": "n8n-nodes-base.set", + "typeVersion": 2, + "position": [140, 2060] + } + ], + "pinData": { + "Date & Time2": [ + { + "json": { + "testData": "2023-03-31T06:31:01.000", + "data": "2023-03-31" + } + } + ], + "Date & Time3": [ + { + "json": { + "testData": "2023-03-31T06:31:01.000", + "data": "2023-03-30" + } + } + ] + }, + "connections": { + "When clicking \"Execute Workflow\"": { + "main": [ + [ + { + "node": "Set", + "type": "main", + "index": 0 + } + ] + ] + }, + "Date & Time": { + "main": [ + [ + { + "node": "Date & Time2", + "type": "main", + "index": 0 + } + ] + ] + }, + "Date & Time1": { + "main": [ + [ + { + "node": "Date & Time3", + "type": "main", + "index": 0 + } + ] + ] + }, + "Set": { + "main": [ + [ + { + "node": "Date & Time", + "type": "main", + "index": 0 + }, + { + "node": "Date & Time1", + "type": "main", + "index": 0 + } + ] + ] + } + }, + "active": false, + "settings": {}, + "versionId": "ff99618e-6e8e-49d8-b899-021caff6d01d", + "id": "151", + "meta": { + "instanceId": "36203ea1ce3cef713fa25999bd9874ae26b9e4c2c3a90a365f2882a154d031d0" + }, + "tags": [] +}