diff --git a/packages/nodes-base/nodes/DateTime/V2/DateTimeV2.node.ts b/packages/nodes-base/nodes/DateTime/V2/DateTimeV2.node.ts index 6a2f13587e..fd2710d03e 100644 --- a/packages/nodes-base/nodes/DateTime/V2/DateTimeV2.node.ts +++ b/packages/nodes-base/nodes/DateTime/V2/DateTimeV2.node.ts @@ -16,6 +16,8 @@ import { AddToDateDescription } from './AddToDateDescription'; import { SubtractFromDateDescription } from './SubtractFromDateDescription'; import { FormatDateDescription } from './FormatDateDescription'; import { RoundDateDescription } from './RoundDateDescription'; +import { GetTimeBetweenDates } from './GetTimeBetweenDates'; +import { DateTime, DurationUnit } from 'luxon'; export class DateTimeV2 implements INodeType { description: INodeTypeDescription; @@ -73,6 +75,7 @@ export class DateTimeV2 implements INodeType { ...SubtractFromDateDescription, ...FormatDateDescription, ...RoundDateDescription, + ...GetTimeBetweenDates, ], }; } @@ -179,6 +182,22 @@ export class DateTimeV2 implements INodeType { } catch { throw new NodeOperationError(this.getNode(), 'Invalid date format'); } + } else if (operation === 'getTimeBetweenDates') { + const startDate = this.getNodeParameter('startDate', i) as string; + const endDate = this.getNodeParameter('endDate', i) as string; + const unit = this.getNodeParameter('units', i) as DurationUnit[]; + const outputFieldName = this.getNodeParameter('outputFieldName', i) as string; + let luxonStartDate; + let luxonEndDate; + + try { + luxonStartDate = DateTime.fromISO(moment.tz(startDate, workflowTimezone).toISOString()); + luxonEndDate = DateTime.fromISO(moment.tz(endDate, workflowTimezone).toISOString()); + } catch { + throw new NodeOperationError(this.getNode(), 'Invalid date format'); + } + const duration = luxonEndDate.diff(luxonStartDate, unit); + responseData.push({ [outputFieldName]: duration.toObject() }); } const executionData = this.helpers.constructExecutionMetaData( this.helpers.returnJsonArray(responseData as IDataObject[]), diff --git a/packages/nodes-base/nodes/DateTime/V2/GetTimeBetweenDates.ts b/packages/nodes-base/nodes/DateTime/V2/GetTimeBetweenDates.ts new file mode 100644 index 0000000000..3315288b37 --- /dev/null +++ b/packages/nodes-base/nodes/DateTime/V2/GetTimeBetweenDates.ts @@ -0,0 +1,83 @@ +import type { INodeProperties } from 'n8n-workflow'; + +export const GetTimeBetweenDates: INodeProperties[] = [ + { + displayName: 'Start Date', + name: 'startDate', + type: 'string', + default: '', + displayOptions: { + show: { + operation: ['getTimeBetweenDates'], + }, + }, + }, + { + displayName: 'End Date', + name: 'endDate', + type: 'string', + default: '', + displayOptions: { + show: { + operation: ['getTimeBetweenDates'], + }, + }, + }, + { + displayName: 'Units', + name: 'units', + type: 'multiOptions', + options: [ + { + name: 'Day', + value: 'day', + }, + { + name: 'Hour', + value: 'hour', + }, + { + name: 'Millisecond', + value: 'millisecond', + }, + { + name: 'Minute', + value: 'minute', + }, + { + name: 'Month', + value: 'month', + }, + { + name: 'Second', + value: 'second', + }, + { + name: 'Week', + value: 'week', + }, + { + name: 'Year', + value: 'year', + }, + ], + displayOptions: { + show: { + operation: ['getTimeBetweenDates'], + }, + }, + default: [], + }, + { + displayName: 'Output Field Name', + name: 'outputFieldName', + type: 'string', + default: 'timeDifference', + description: 'Name of the field to put the output in', + displayOptions: { + show: { + operation: ['getTimeBetweenDates'], + }, + }, + }, +];