From 38febb87c46397c65a99ec849c794bdc32c33688 Mon Sep 17 00:00:00 2001 From: agobrech Date: Wed, 5 Apr 2023 16:47:44 +0200 Subject: [PATCH] Feat format date operation --- .../nodes/DateTime/V2/DateTimeV2.node.ts | 17 +++ .../DateTime/V2/FormatDateDescription.ts | 107 ++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 packages/nodes-base/nodes/DateTime/V2/FormatDateDescription.ts diff --git a/packages/nodes-base/nodes/DateTime/V2/DateTimeV2.node.ts b/packages/nodes-base/nodes/DateTime/V2/DateTimeV2.node.ts index c14492887a..ff919520d8 100644 --- a/packages/nodes-base/nodes/DateTime/V2/DateTimeV2.node.ts +++ b/packages/nodes-base/nodes/DateTime/V2/DateTimeV2.node.ts @@ -14,6 +14,7 @@ import moment from 'moment-timezone'; import { CurrentDateDescription } from './CurrentDateDescription'; import { AddToDateDescription } from './AddToDateDescription'; import { SubtractFromDateDescription } from './SubtractFromDateDescription'; +import { FormatDateDescription } from './FormatDateDescription'; export class DateTimeV2 implements INodeType { description: INodeTypeDescription; @@ -69,6 +70,7 @@ export class DateTimeV2 implements INodeType { ...CurrentDateDescription, ...AddToDateDescription, ...SubtractFromDateDescription, + ...FormatDateDescription, ], }; } @@ -138,6 +140,21 @@ export class DateTimeV2 implements INodeType { } catch { throw new NodeOperationError(this.getNode(), 'Invalid date format'); } + } else if (operation === 'formatDate') { + const date = this.getNodeParameter('date', i) as string; + const format = this.getNodeParameter('format', i) as string; + const outputFieldName = this.getNodeParameter('outputFieldName', i) as string; + try { + const momentDate = moment.tz(date, workflowTimezone); + if (format === 'custom') { + const customFormat = this.getNodeParameter('customFormat', i) as string; + responseData.push({ [outputFieldName]: momentDate.format(customFormat) }); + } else { + responseData.push({ [outputFieldName]: momentDate.format(format) }); + } + } catch { + throw new NodeOperationError(this.getNode(), 'Invalid date format'); + } } const executionData = this.helpers.constructExecutionMetaData( this.helpers.returnJsonArray(responseData as IDataObject[]), diff --git a/packages/nodes-base/nodes/DateTime/V2/FormatDateDescription.ts b/packages/nodes-base/nodes/DateTime/V2/FormatDateDescription.ts new file mode 100644 index 0000000000..81a3eba00a --- /dev/null +++ b/packages/nodes-base/nodes/DateTime/V2/FormatDateDescription.ts @@ -0,0 +1,107 @@ +import type { INodeProperties } from 'n8n-workflow'; + +export const FormatDateDescription: INodeProperties[] = [ + { + displayName: + "You can also do this using an expression, e.g. {{your_date.format('yyyy-MM-dd')}}. More info", + name: 'notice', + type: 'notice', + default: '', + displayOptions: { + show: { + operation: ['formatDate'], + }, + }, + }, + { + displayName: 'Date', + name: 'date', + type: 'string', + description: 'The date that you want to format', + default: '', + displayOptions: { + show: { + operation: ['formatDate'], + }, + }, + }, + { + displayName: 'Format', + name: 'Format', + type: 'options', + displayOptions: { + show: { + action: ['format'], + custom: [false], + }, + }, + // eslint-disable-next-line n8n-nodes-base/node-param-options-type-unsorted-items + options: [ + { + name: 'Custom Format', + value: 'custom', + }, + { + name: 'MM/DD/YYYY', + value: 'MM/DD/YYYY', + description: 'Example: 09/04/1986', + }, + { + name: 'YYYY/MM/DD', + value: 'YYYY/MM/DD', + description: 'Example: 1986/04/09', + }, + { + name: 'MMMM DD YYYY', + value: 'MMMM DD YYYY', + description: 'Example: April 09 1986', + }, + { + name: 'MM-DD-YYYY', + value: 'MM-DD-YYYY', + description: 'Example: 09-04-1986', + }, + { + name: 'YYYY-MM-DD', + value: 'YYYY-MM-DD', + description: 'Example: 1986-04-09', + }, + { + name: 'Unix Timestamp', + value: 'X', + description: 'Example: 513388800.879', + }, + { + name: 'Unix Ms Timestamp', + value: 'x', + description: 'Example: 513388800', + }, + ], + default: 'MM/DD/YYYY', + description: 'The format to convert the date to', + }, + { + displayName: 'Custom Format', + name: 'customFormat', + type: 'string', + displayOptions: { + show: { + format: ['custom'], + }, + }, + hint: 'List of special tokens More info', + default: 'yyyy-MM-dd', + }, + { + displayName: 'Output Field Name', + name: 'outputFieldName', + type: 'string', + default: 'formattedDate', + description: 'Name of the field to put the output in', + displayOptions: { + show: { + operation: ['formatDate'], + }, + }, + }, +];