Feat get in between date operation

This commit is contained in:
agobrech 2023-04-06 10:17:32 +02:00
parent d0038812d7
commit 97d486c52f
2 changed files with 102 additions and 0 deletions

View file

@ -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[]),

View file

@ -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'],
},
},
},
];