diff --git a/packages/nodes-base/nodes/DateTime/V2/DateTimeV2.node.ts b/packages/nodes-base/nodes/DateTime/V2/DateTimeV2.node.ts index 75ecf9fc84..c14492887a 100644 --- a/packages/nodes-base/nodes/DateTime/V2/DateTimeV2.node.ts +++ b/packages/nodes-base/nodes/DateTime/V2/DateTimeV2.node.ts @@ -13,6 +13,7 @@ import { NodeOperationError } from 'n8n-workflow'; import moment from 'moment-timezone'; import { CurrentDateDescription } from './CurrentDateDescription'; import { AddToDateDescription } from './AddToDateDescription'; +import { SubtractFromDateDescription } from './SubtractFromDateDescription'; export class DateTimeV2 implements INodeType { description: INodeTypeDescription; @@ -67,6 +68,7 @@ export class DateTimeV2 implements INodeType { }, ...CurrentDateDescription, ...AddToDateDescription, + ...SubtractFromDateDescription, ], }; } @@ -121,7 +123,21 @@ export class DateTimeV2 implements INodeType { } catch { throw new NodeOperationError(this.getNode(), 'Invalid date format'); } - } else if (true) { + } else if (operation === 'subtractFromDate') { + const subtractFromDate = this.getNodeParameter('subtractFromDate', i) as string; + const timeUnit = this.getNodeParameter('timeUnit', i) as string; + const duration = this.getNodeParameter('duration', i) as number; + const outputFieldName = this.getNodeParameter('outputFieldName', i) as string; + try { + const dateToAdd = moment.tz(subtractFromDate, workflowTimezone); + const returnedDate = moment(dateToAdd).subtract( + duration, + timeUnit as moment.unitOfTime.DurationConstructor, + ); + responseData.push({ [outputFieldName]: returnedDate }); + } 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/SubtractFromDateDescription.ts b/packages/nodes-base/nodes/DateTime/V2/SubtractFromDateDescription.ts new file mode 100644 index 0000000000..51ed1dabc6 --- /dev/null +++ b/packages/nodes-base/nodes/DateTime/V2/SubtractFromDateDescription.ts @@ -0,0 +1,105 @@ +import type { INodeProperties } from 'n8n-workflow'; + +export const SubtractFromDateDescription: INodeProperties[] = [ + { + displayName: + "You can also do this using an expression, e.g. {{your_date.minus(5, 'minutes')}}. More info", + name: 'notice', + type: 'notice', + default: '', + displayOptions: { + show: { + operation: ['subtractFromDate'], + }, + }, + }, + { + displayName: 'Date to Subtract From', + name: 'subtractFromDate', + type: 'string', + description: 'The date that you want to change', + default: '', + displayOptions: { + show: { + operation: ['subtractFromDate'], + }, + }, + required: true, + }, + { + displayName: 'Time Unit to Subtract', + name: 'timeUnit', + description: 'Time unit for Duration parameter above', + displayOptions: { + show: { + operation: ['subtractFromDate'], + }, + }, + type: 'options', + // eslint-disable-next-line n8n-nodes-base/node-param-options-type-unsorted-items + options: [ + { + name: 'Quarters', + value: 'quarters', + }, + { + name: 'Years', + value: 'years', + }, + { + name: 'Months', + value: 'months', + }, + { + name: 'Weeks', + value: 'weeks', + }, + { + name: 'Days', + value: 'days', + }, + { + name: 'Hours', + value: 'hours', + }, + { + name: 'Minutes', + value: 'minutes', + }, + { + name: 'Seconds', + value: 'seconds', + }, + { + name: 'Milliseconds', + value: 'milliseconds', + }, + ], + default: 'days', + required: true, + }, + { + displayName: 'Duration', + name: 'duration', + type: 'number', + description: 'The number of time units to subtract from the date', + default: 0, + displayOptions: { + show: { + operation: ['subtractFromDate'], + }, + }, + }, + { + displayName: 'Output Field Name', + name: 'outputFieldName', + type: 'string', + default: 'newDate', + description: 'Name of the field to put the output in', + displayOptions: { + show: { + operation: ['subtractFromDate'], + }, + }, + }, +];