mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
feat add to date operator
This commit is contained in:
parent
f32366984f
commit
a341770909
105
packages/nodes-base/nodes/DateTime/V2/AddToDateDescription.ts
Normal file
105
packages/nodes-base/nodes/DateTime/V2/AddToDateDescription.ts
Normal 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'],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
|
@ -8,6 +8,7 @@ import type {
|
||||||
INodeTypeBaseDescription,
|
INodeTypeBaseDescription,
|
||||||
INodeTypeDescription,
|
INodeTypeDescription,
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
|
import { NodeOperationError } from 'n8n-workflow';
|
||||||
|
|
||||||
import moment from 'moment-timezone';
|
import moment from 'moment-timezone';
|
||||||
import { CurrentDateDescription } from './CurrentDateDescription';
|
import { CurrentDateDescription } from './CurrentDateDescription';
|
||||||
|
@ -95,6 +96,7 @@ export class DateTimeV2 implements INodeType {
|
||||||
const responseData = [];
|
const responseData = [];
|
||||||
const operation = this.getNodeParameter('operation', 0);
|
const operation = this.getNodeParameter('operation', 0);
|
||||||
const workflowTimezone = this.getTimezone();
|
const workflowTimezone = this.getTimezone();
|
||||||
|
|
||||||
for (let i = 0; i < items.length; i++) {
|
for (let i = 0; i < items.length; i++) {
|
||||||
if (operation === 'getCurrentDate') {
|
if (operation === 'getCurrentDate') {
|
||||||
const includeTime = this.getNodeParameter('includeTime', i) as boolean;
|
const includeTime = this.getNodeParameter('includeTime', i) as boolean;
|
||||||
|
@ -105,6 +107,21 @@ export class DateTimeV2 implements INodeType {
|
||||||
: { [outputFieldName]: moment().startOf('day').tz(workflowTimezone) },
|
: { [outputFieldName]: moment().startOf('day').tz(workflowTimezone) },
|
||||||
);
|
);
|
||||||
} else if (operation === 'addToDate') {
|
} 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(
|
const executionData = this.helpers.constructExecutionMetaData(
|
||||||
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
||||||
|
|
Loading…
Reference in a new issue