feat(Date & Time Node): Option to include other fields in output item (#7661)

Github issue / Community forum post (link here to close automatically):
Community:
https://community.n8n.io/t/date-time-deletes-incoming-props/27492/3
GH Issue: https://github.com/n8n-io/n8n/issues/7646

---------

Co-authored-by: Marcus <marcus@n8n.io>
This commit is contained in:
Michael Kret 2023-11-09 17:57:33 +02:00 committed by GitHub
parent 18623c7376
commit aea3c50131
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 613 additions and 116 deletions

View file

@ -2,7 +2,6 @@ import type { INodeTypeBaseDescription, IVersionedNodeType } from 'n8n-workflow'
import { VersionedNodeType } from 'n8n-workflow';
import { DateTimeV1 } from './V1/DateTimeV1.node';
import { DateTimeV2 } from './V2/DateTimeV2.node';
export class DateTime extends VersionedNodeType {

View file

@ -1,4 +1,5 @@
import type { INodeProperties } from 'n8n-workflow';
import { includeInputFields } from './common.descriptions';
export const AddToDateDescription: INodeProperties[] = [
{
@ -102,4 +103,17 @@ export const AddToDateDescription: INodeProperties[] = [
},
},
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
displayOptions: {
show: {
operation: ['addToDate'],
},
},
default: {},
options: [includeInputFields],
},
];

View file

@ -1,4 +1,5 @@
import type { INodeProperties } from 'n8n-workflow';
import { includeInputFields } from './common.descriptions';
export const CurrentDateDescription: INodeProperties[] = [
{
@ -50,6 +51,7 @@ export const CurrentDateDescription: INodeProperties[] = [
},
default: {},
options: [
includeInputFields,
{
displayName: 'Timezone',
name: 'timezone',

View file

@ -1,5 +1,4 @@
import type {
IDataObject,
IExecuteFunctions,
INodeExecutionData,
INodeType,
@ -84,11 +83,26 @@ export class DateTimeV2 implements INodeType {
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: INodeExecutionData[] = [];
const responseData = [];
const operation = this.getNodeParameter('operation', 0);
const workflowTimezone = this.getTimezone();
const includeInputFields = this.getNodeParameter(
'options.includeInputFields',
0,
false,
) as boolean;
const copyShallow = (item: INodeExecutionData) => ({
json: { ...item.json },
binary: item.binary,
});
for (let i = 0; i < items.length; i++) {
try {
const item: INodeExecutionData = includeInputFields ? copyShallow(items[i]) : { json: {} };
item.pairedItem = {
item: i,
};
if (operation === 'getCurrentDate') {
const includeTime = this.getNodeParameter('includeTime', i) as boolean;
const outputFieldName = this.getNodeParameter('outputFieldName', i) as string;
@ -103,13 +117,13 @@ export class DateTimeV2 implements INodeType {
`The timezone ${newLocal} is not valid. Please check the timezone.`,
);
}
responseData.push(
includeTime
? { [outputFieldName]: DateTime.now().setZone(newLocal).toString() }
: {
[outputFieldName]: DateTime.now().setZone(newLocal).startOf('day').toString(),
},
);
if (includeTime) {
item.json[outputFieldName] = DateTime.now().setZone(newLocal).toString();
} else {
item.json[outputFieldName] = DateTime.now().setZone(newLocal).startOf('day').toString();
}
returnData.push(item);
} else if (operation === 'addToDate') {
const addToDate = this.getNodeParameter('magnitude', i) as string;
const timeUnit = this.getNodeParameter('timeUnit', i) as string;
@ -118,7 +132,9 @@ export class DateTimeV2 implements INodeType {
const dateToAdd = parseDate.call(this, addToDate, workflowTimezone);
const returnedDate = dateToAdd.plus({ [timeUnit]: duration });
responseData.push({ [outputFieldName]: returnedDate.toString() });
item.json[outputFieldName] = returnedDate.toString();
returnData.push(item);
} else if (operation === 'subtractFromDate') {
const subtractFromDate = this.getNodeParameter('magnitude', i) as string;
const timeUnit = this.getNodeParameter('timeUnit', i) as string;
@ -127,7 +143,9 @@ export class DateTimeV2 implements INodeType {
const dateToAdd = parseDate.call(this, subtractFromDate, workflowTimezone);
const returnedDate = dateToAdd.minus({ [timeUnit]: duration });
responseData.push({ [outputFieldName]: returnedDate.toString() });
item.json[outputFieldName] = returnedDate.toString();
returnData.push(item);
} else if (operation === 'formatDate') {
const date = this.getNodeParameter('date', i) as string;
const format = this.getNodeParameter('format', i) as string;
@ -135,24 +153,19 @@ export class DateTimeV2 implements INodeType {
const { timezone } = this.getNodeParameter('options', i) as { timezone: boolean };
if (date === null || date === undefined) {
responseData.push({
[outputFieldName]: date,
});
item.json[outputFieldName] = date;
} else {
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),
});
item.json[outputFieldName] = dateLuxon.toFormat(customFormat);
} else {
responseData.push({
[outputFieldName]: dateLuxon.toFormat(format),
});
item.json[outputFieldName] = dateLuxon.toFormat(format);
}
}
returnData.push(item);
} else if (operation === 'roundDate') {
const date = this.getNodeParameter('date', i) as string;
const mode = this.getNodeParameter('mode', i) as string;
@ -162,18 +175,15 @@ export class DateTimeV2 implements INodeType {
if (mode === 'roundDown') {
const toNearest = this.getNodeParameter('toNearest', i) as string;
responseData.push({
[outputFieldName]: dateLuxon.startOf(toNearest as DateTimeUnit).toString(),
});
item.json[outputFieldName] = dateLuxon.startOf(toNearest as DateTimeUnit).toString();
} else if (mode === 'roundUp') {
const to = this.getNodeParameter('to', i) as string;
responseData.push({
[outputFieldName]: dateLuxon
item.json[outputFieldName] = dateLuxon
.plus({ [to]: 1 })
.startOf(to as DateTimeUnit)
.toString(),
});
.toString();
}
returnData.push(item);
} else if (operation === 'getTimeBetweenDates') {
const startDate = this.getNodeParameter('startDate', i) as string;
const endDate = this.getNodeParameter('endDate', i) as string;
@ -186,13 +196,12 @@ export class DateTimeV2 implements INodeType {
const luxonStartDate = parseDate.call(this, startDate, workflowTimezone);
const luxonEndDate = parseDate.call(this, endDate, workflowTimezone);
const duration = luxonEndDate.diff(luxonStartDate, unit);
isoString
? responseData.push({
[outputFieldName]: duration.toString(),
})
: responseData.push({
[outputFieldName]: duration.toObject(),
});
if (isoString) {
item.json[outputFieldName] = duration.toString();
} else {
item.json[outputFieldName] = duration.toObject();
}
returnData.push(item);
} else if (operation === 'extractDate') {
const date = this.getNodeParameter('date', i) as string | DateTime;
const outputFieldName = this.getNodeParameter('outputFieldName', i) as string;
@ -200,18 +209,16 @@ export class DateTimeV2 implements INodeType {
const parsedDate = parseDate.call(this, date, workflowTimezone);
const selectedPart = part === 'week' ? parsedDate.weekNumber : parsedDate.get(part);
responseData.push({ [outputFieldName]: selectedPart });
item.json[outputFieldName] = selectedPart;
returnData.push(item);
}
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ json: { error: error.message } });
continue;
}
throw new NodeOperationError(this.getNode(), error, { itemIndex: i });
}
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(responseData as IDataObject[]),
{
itemData: { item: i },
},
);
returnData.push(...executionData);
// Reset responseData
responseData.length = 0;
}
return [returnData];
}

View file

@ -1,4 +1,5 @@
import type { INodeProperties } from 'n8n-workflow';
import { includeInputFields } from './common.descriptions';
export const ExtractDateDescription: INodeProperties[] = [
{
@ -79,4 +80,17 @@ export const ExtractDateDescription: INodeProperties[] = [
},
},
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
displayOptions: {
show: {
operation: ['extractDate'],
},
},
default: {},
options: [includeInputFields],
},
];

View file

@ -1,4 +1,5 @@
import type { INodeProperties } from 'n8n-workflow';
import { includeInputFields } from './common.descriptions';
export const FormatDateDescription: INodeProperties[] = [
{
@ -117,6 +118,7 @@ export const FormatDateDescription: INodeProperties[] = [
},
default: {},
options: [
includeInputFields,
{
displayName: 'Use Workflow Timezone',
name: 'timezone',

View file

@ -1,4 +1,5 @@
import type { INodeProperties } from 'n8n-workflow';
import { includeInputFields } from './common.descriptions';
export const GetTimeBetweenDatesDescription: INodeProperties[] = [
{
@ -93,6 +94,7 @@ export const GetTimeBetweenDatesDescription: INodeProperties[] = [
},
default: {},
options: [
includeInputFields,
{
displayName: 'Output as ISO String',
name: 'isoString',

View file

@ -1,4 +1,5 @@
import type { INodeProperties } from 'n8n-workflow';
import { includeInputFields } from './common.descriptions';
export const RoundDateDescription: INodeProperties[] = [
{
@ -119,4 +120,17 @@ export const RoundDateDescription: INodeProperties[] = [
},
},
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
displayOptions: {
show: {
operation: ['roundDate'],
},
},
default: {},
options: [includeInputFields],
},
];

View file

@ -1,4 +1,5 @@
import type { INodeProperties } from 'n8n-workflow';
import { includeInputFields } from './common.descriptions';
export const SubtractFromDateDescription: INodeProperties[] = [
{
@ -102,4 +103,17 @@ export const SubtractFromDateDescription: INodeProperties[] = [
},
},
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
displayOptions: {
show: {
operation: ['subtractFromDate'],
},
},
default: {},
options: [includeInputFields],
},
];

View file

@ -0,0 +1,9 @@
import type { INodeProperties } from 'n8n-workflow';
export const includeInputFields: INodeProperties = {
displayName: 'Include Input Fields',
name: 'includeInputFields',
type: 'boolean',
default: false,
description: 'Whether to include all fields of the input item in the output item',
};

View file

@ -0,0 +1,420 @@
{
"name": "My workflow 38",
"nodes": [
{
"parameters": {},
"id": "f1242de8-11fc-4849-a4a2-c82fa75731a7",
"name": "When clicking \"Execute Workflow\"",
"type": "n8n-nodes-base.manualTrigger",
"typeVersion": 1,
"position": [
120,
800
]
},
{
"parameters": {
"values": {
"number": [
{
"name": "dateMilis",
"value": 1682918315906
},
{
"name": "dateMilisFloat",
"value": 1682918315.906
},
{
"name": "dateUnix",
"value": 1682918315
}
],
"string": [
{
"name": "dateMilisStr",
"value": "1682918315906"
},
{
"name": "dateMilisFloatStr",
"value": "1682918315.906"
},
{
"name": "dateUnixStr",
"value": "1682918315"
}
]
},
"options": {}
},
"id": "7208910f-fd33-42b9-bcf2-14bb0b0d157d",
"name": "Set",
"type": "n8n-nodes-base.set",
"typeVersion": 2,
"position": [
320,
800
]
},
{
"parameters": {
"operation": "formatDate",
"date": "={{ $json.dateMilis }}",
"format": "yyyy/MM/dd",
"outputFieldName": "data",
"options": {}
},
"id": "fb66a2cc-95b9-4917-999b-847ab61a7938",
"name": "Date & Time6",
"type": "n8n-nodes-base.dateTime",
"typeVersion": 2,
"position": [
700,
360
]
},
{
"parameters": {
"operation": "formatDate",
"date": "={{ $json.dateMilisFloat }}",
"format": "yyyy/MM/dd",
"outputFieldName": "data",
"options": {}
},
"id": "b94eb54a-1952-4f33-816a-8ffa47c7bb42",
"name": "Date & Time",
"type": "n8n-nodes-base.dateTime",
"typeVersion": 2,
"position": [
700,
720
]
},
{
"parameters": {
"operation": "formatDate",
"date": "={{ $json.dateUnix }}",
"format": "yyyy/MM/dd",
"outputFieldName": "data",
"options": {}
},
"id": "7f0af92d-8064-4af0-8c2c-a21b70f69ac1",
"name": "Date & Time1",
"type": "n8n-nodes-base.dateTime",
"typeVersion": 2,
"position": [
700,
1080
]
},
{
"parameters": {},
"id": "1e90549e-f4d4-46a1-bc9b-617befff2319",
"name": "No Operation, do nothing",
"type": "n8n-nodes-base.noOp",
"typeVersion": 1,
"position": [
900,
360
]
},
{
"parameters": {},
"id": "ec07d232-e513-4f09-8cb1-e8e4853bcebc",
"name": "No Operation, do nothing1",
"type": "n8n-nodes-base.noOp",
"typeVersion": 1,
"position": [
900,
720
]
},
{
"parameters": {},
"id": "f9839c73-ed5e-4ae0-bc1e-5a1914ac8b55",
"name": "No Operation, do nothing2",
"type": "n8n-nodes-base.noOp",
"typeVersion": 1,
"position": [
900,
1080
]
},
{
"parameters": {
"operation": "formatDate",
"date": "={{ $json.dateMilis }}",
"format": "yyyy/MM/dd",
"outputFieldName": "data",
"options": {
"includeInputFields": true
}
},
"id": "bfce6276-2de4-4783-bc7f-d82fab23c2af",
"name": "Date & Time7",
"type": "n8n-nodes-base.dateTime",
"typeVersion": 2,
"position": [
700,
500
]
},
{
"parameters": {
"operation": "formatDate",
"date": "={{ $json.dateMilisFloat }}",
"format": "yyyy/MM/dd",
"outputFieldName": "data",
"options": {
"includeInputFields": true
}
},
"id": "a7aad9c9-20ff-4353-9445-80366fad9431",
"name": "Date & Time2",
"type": "n8n-nodes-base.dateTime",
"typeVersion": 2,
"position": [
700,
860
]
},
{
"parameters": {
"operation": "formatDate",
"date": "={{ $json.dateUnix }}",
"format": "yyyy/MM/dd",
"outputFieldName": "data",
"options": {
"includeInputFields": true
}
},
"id": "3b9e3ad7-0d00-4598-b796-23bb206e75db",
"name": "Date & Time3",
"type": "n8n-nodes-base.dateTime",
"typeVersion": 2,
"position": [
700,
1240
]
},
{
"parameters": {},
"id": "8a3cdde0-b1de-46dc-a8c6-74cc174fd4a1",
"name": "No Operation, do nothing3",
"type": "n8n-nodes-base.noOp",
"typeVersion": 1,
"position": [
900,
1240
]
},
{
"parameters": {},
"id": "54b4d584-0874-411b-9c98-8194c0928588",
"name": "No Operation, do nothing4",
"type": "n8n-nodes-base.noOp",
"typeVersion": 1,
"position": [
900,
860
]
},
{
"parameters": {},
"id": "4a4e7647-6c6e-4a24-9032-829e5f495337",
"name": "No Operation, do nothing5",
"type": "n8n-nodes-base.noOp",
"typeVersion": 1,
"position": [
900,
500
]
}
],
"pinData": {
"No Operation, do nothing2": [
{
"json": {
"data": "2023/05/01"
}
}
],
"No Operation, do nothing1": [
{
"json": {
"data": "2023/05/01"
}
}
],
"No Operation, do nothing": [
{
"json": {
"data": "2023/05/01"
}
}
],
"No Operation, do nothing5": [
{
"json": {
"dateMilis": 1682918315906,
"dateMilisFloat": 1682918315.906,
"dateUnix": 1682918315,
"dateMilisStr": "1682918315906",
"dateMilisFloatStr": "1682918315.906",
"dateUnixStr": "1682918315",
"data": "2023/05/01"
}
}
],
"No Operation, do nothing4": [
{
"json": {
"dateMilis": 1682918315906,
"dateMilisFloat": 1682918315.906,
"dateUnix": 1682918315,
"dateMilisStr": "1682918315906",
"dateMilisFloatStr": "1682918315.906",
"dateUnixStr": "1682918315",
"data": "2023/05/01"
}
}
],
"No Operation, do nothing3": [
{
"json": {
"dateMilis": 1682918315906,
"dateMilisFloat": 1682918315.906,
"dateUnix": 1682918315,
"dateMilisStr": "1682918315906",
"dateMilisFloatStr": "1682918315.906",
"dateUnixStr": "1682918315",
"data": "2023/05/01"
}
}
]
},
"connections": {
"When clicking \"Execute Workflow\"": {
"main": [
[
{
"node": "Set",
"type": "main",
"index": 0
}
]
]
},
"Set": {
"main": [
[
{
"node": "Date & Time6",
"type": "main",
"index": 0
},
{
"node": "Date & Time",
"type": "main",
"index": 0
},
{
"node": "Date & Time1",
"type": "main",
"index": 0
},
{
"node": "Date & Time7",
"type": "main",
"index": 0
},
{
"node": "Date & Time2",
"type": "main",
"index": 0
},
{
"node": "Date & Time3",
"type": "main",
"index": 0
}
]
]
},
"Date & Time6": {
"main": [
[
{
"node": "No Operation, do nothing",
"type": "main",
"index": 0
}
]
]
},
"Date & Time1": {
"main": [
[
{
"node": "No Operation, do nothing2",
"type": "main",
"index": 0
}
]
]
},
"Date & Time": {
"main": [
[
{
"node": "No Operation, do nothing1",
"type": "main",
"index": 0
}
]
]
},
"Date & Time3": {
"main": [
[
{
"node": "No Operation, do nothing3",
"type": "main",
"index": 0
}
]
]
},
"Date & Time2": {
"main": [
[
{
"node": "No Operation, do nothing4",
"type": "main",
"index": 0
}
]
]
},
"Date & Time7": {
"main": [
[
{
"node": "No Operation, do nothing5",
"type": "main",
"index": 0
}
]
]
}
},
"active": false,
"settings": {
"executionOrder": "v1"
},
"versionId": "419a6926-5d06-4ba5-b261-410e124996de",
"id": "hW8KyOyaCN5fGQru",
"meta": {
"instanceId": "b888bd11cd1ddbb95450babf3e199556799d999b896f650de768b8370ee50363"
},
"tags": []
}