feat add to date operator

This commit is contained in:
agobrech 2023-04-05 15:33:44 +02:00
parent f32366984f
commit a341770909
2 changed files with 122 additions and 0 deletions

View file

@ -0,0 +1,105 @@
import type { INodeProperties } from 'n8n-workflow';
export const AddToDateDescription: INodeProperties[] = [
{
displayName:
"You can also do this using an expression, e.g. {{ your_date.plus(5, 'minutes') }}. More info",
name: 'notice',
type: 'notice',
default: '',
displayOptions: {
show: {
operation: ['addToDate'],
},
},
},
{
displayName: 'Date to Add To',
name: 'addToDate',
type: 'string',
description: 'The date that you want to change',
default: '',
displayOptions: {
show: {
operation: ['addToDate'],
},
},
required: true,
},
{
displayName: 'Time Unit to Add',
name: 'timeUnit',
description: 'Time unit for Duration parameter above',
displayOptions: {
show: {
operation: ['addToDate'],
},
},
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 add to the date',
default: 0,
displayOptions: {
show: {
operation: ['addToDate'],
},
},
},
{
displayName: 'Output Field Name',
name: 'outputFieldName',
type: 'string',
default: 'new_date',
description: 'Name of the field to put the output in',
displayOptions: {
show: {
operation: ['addToDate'],
},
},
},
];

View file

@ -8,6 +8,7 @@ import type {
INodeTypeBaseDescription,
INodeTypeDescription,
} from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
import moment from 'moment-timezone';
import { CurrentDateDescription } from './CurrentDateDescription';
@ -95,6 +96,7 @@ export class DateTimeV2 implements INodeType {
const responseData = [];
const operation = this.getNodeParameter('operation', 0);
const workflowTimezone = this.getTimezone();
for (let i = 0; i < items.length; i++) {
if (operation === 'getCurrentDate') {
const includeTime = this.getNodeParameter('includeTime', i) as boolean;
@ -105,6 +107,21 @@ export class DateTimeV2 implements INodeType {
: { [outputFieldName]: moment().startOf('day').tz(workflowTimezone) },
);
} else if (operation === 'addToDate') {
const addToDate = this.getNodeParameter('addToDate', 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(addToDate, workflowTimezone);
const returnedDate = moment(dateToAdd).add(
duration,
timeUnit as moment.unitOfTime.DurationConstructor,
);
console.log(returnedDate);
responseData.push({ [outputFieldName]: returnedDate });
} catch {
throw new NodeOperationError(this.getNode(), 'Invalid date format');
}
}
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(responseData as IDataObject[]),