mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
Feat subtract to date operation
This commit is contained in:
parent
99c84a70fb
commit
20dd9e0e22
|
@ -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[]),
|
||||
|
|
|
@ -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. <code>{{your_date.minus(5, 'minutes')}}</code>. <a target='_blank' href='https://docs.n8n.io/code-examples/expressions/luxon/'>More info</a>",
|
||||
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'],
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
Loading…
Reference in a new issue