2023-04-04 05:45:06 -07:00
|
|
|
import type {
|
2023-04-04 08:08:49 -07:00
|
|
|
IDataObject,
|
2023-04-04 05:45:06 -07:00
|
|
|
IExecuteFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeBaseDescription,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
2023-04-05 06:33:44 -07:00
|
|
|
import { NodeOperationError } from 'n8n-workflow';
|
2023-04-04 05:45:06 -07:00
|
|
|
|
|
|
|
import moment from 'moment-timezone';
|
2023-04-04 08:08:49 -07:00
|
|
|
import { CurrentDateDescription } from './CurrentDateDescription';
|
|
|
|
import { AddToDateDescription } from './AddToDateDescription';
|
2023-04-05 07:06:04 -07:00
|
|
|
import { SubtractFromDateDescription } from './SubtractFromDateDescription';
|
2023-04-05 07:47:44 -07:00
|
|
|
import { FormatDateDescription } from './FormatDateDescription';
|
2023-04-04 05:45:06 -07:00
|
|
|
|
|
|
|
export class DateTimeV2 implements INodeType {
|
|
|
|
description: INodeTypeDescription;
|
|
|
|
|
|
|
|
constructor(baseDescription: INodeTypeBaseDescription) {
|
|
|
|
this.description = {
|
|
|
|
...baseDescription,
|
|
|
|
version: 2,
|
|
|
|
defaults: {
|
|
|
|
name: 'Date & Time',
|
|
|
|
color: '#408000',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
properties: [
|
|
|
|
{
|
2023-04-04 08:08:49 -07:00
|
|
|
displayName: 'Operation',
|
|
|
|
name: 'operation',
|
2023-04-04 05:45:06 -07:00
|
|
|
type: 'options',
|
2023-04-04 08:08:49 -07:00
|
|
|
noDataExpression: true,
|
2023-04-04 05:45:06 -07:00
|
|
|
options: [
|
|
|
|
{
|
2023-04-04 06:18:07 -07:00
|
|
|
name: 'Add to a Date',
|
|
|
|
value: 'addToDate',
|
2023-04-04 05:45:06 -07:00
|
|
|
},
|
|
|
|
{
|
2023-04-04 06:18:07 -07:00
|
|
|
name: 'Extract Part of a Date',
|
|
|
|
value: 'extractDate',
|
2023-04-04 05:45:06 -07:00
|
|
|
},
|
|
|
|
{
|
2023-04-04 06:18:07 -07:00
|
|
|
name: 'Format a Date',
|
|
|
|
value: 'formatDate',
|
2023-04-04 05:45:06 -07:00
|
|
|
},
|
|
|
|
{
|
2023-04-04 06:18:07 -07:00
|
|
|
name: 'Get Current Date',
|
|
|
|
value: 'getCurrentDate',
|
2023-04-04 05:45:06 -07:00
|
|
|
},
|
|
|
|
{
|
2023-04-04 06:18:07 -07:00
|
|
|
name: 'Get Time Between Dates',
|
|
|
|
value: 'getTimeBetweenDates',
|
2023-04-04 05:45:06 -07:00
|
|
|
},
|
|
|
|
{
|
2023-04-04 06:18:07 -07:00
|
|
|
name: 'Round a Date',
|
|
|
|
value: 'roundDate',
|
2023-04-04 05:45:06 -07:00
|
|
|
},
|
|
|
|
{
|
2023-04-04 06:18:07 -07:00
|
|
|
name: 'Subtract From a Date',
|
|
|
|
value: 'subtractFromDate',
|
2023-04-04 05:45:06 -07:00
|
|
|
},
|
|
|
|
],
|
2023-04-04 06:18:07 -07:00
|
|
|
default: 'getCurrentDate',
|
2023-04-04 05:45:06 -07:00
|
|
|
},
|
2023-04-04 08:08:49 -07:00
|
|
|
...CurrentDateDescription,
|
2023-04-04 08:33:00 -07:00
|
|
|
...AddToDateDescription,
|
2023-04-05 07:06:04 -07:00
|
|
|
...SubtractFromDateDescription,
|
2023-04-05 07:47:44 -07:00
|
|
|
...FormatDateDescription,
|
2023-04-04 05:45:06 -07:00
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
// Get all the timezones to display them to user so that he can
|
|
|
|
// select them easily
|
|
|
|
async getTimezones(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
for (const timezone of moment.tz.names()) {
|
|
|
|
const timezoneName = timezone;
|
|
|
|
const timezoneId = timezone;
|
|
|
|
returnData.push({
|
|
|
|
name: timezoneName,
|
|
|
|
value: timezoneId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-04-04 08:08:49 -07:00
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
const returnData: INodeExecutionData[] = [];
|
|
|
|
const responseData = [];
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2023-04-04 08:33:00 -07:00
|
|
|
const workflowTimezone = this.getTimezone();
|
2023-04-05 06:33:44 -07:00
|
|
|
|
2023-04-04 08:08:49 -07:00
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
if (operation === 'getCurrentDate') {
|
|
|
|
const includeTime = this.getNodeParameter('includeTime', i) as boolean;
|
|
|
|
const outputFieldName = this.getNodeParameter('outputFieldName', i) as string;
|
|
|
|
responseData.push(
|
|
|
|
includeTime
|
2023-04-04 08:33:00 -07:00
|
|
|
? { [outputFieldName]: moment.tz(workflowTimezone) }
|
|
|
|
: { [outputFieldName]: moment().startOf('day').tz(workflowTimezone) },
|
2023-04-04 08:08:49 -07:00
|
|
|
);
|
2023-04-04 08:33:00 -07:00
|
|
|
} else if (operation === 'addToDate') {
|
2023-04-05 06:33:44 -07:00
|
|
|
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,
|
|
|
|
);
|
|
|
|
responseData.push({ [outputFieldName]: returnedDate });
|
|
|
|
} catch {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'Invalid date format');
|
|
|
|
}
|
2023-04-05 07:06:04 -07:00
|
|
|
} 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');
|
|
|
|
}
|
2023-04-05 07:47:44 -07:00
|
|
|
} else if (operation === 'formatDate') {
|
|
|
|
const date = this.getNodeParameter('date', i) as string;
|
|
|
|
const format = this.getNodeParameter('format', i) as string;
|
|
|
|
const outputFieldName = this.getNodeParameter('outputFieldName', i) as string;
|
|
|
|
try {
|
|
|
|
const momentDate = moment.tz(date, workflowTimezone);
|
|
|
|
if (format === 'custom') {
|
|
|
|
const customFormat = this.getNodeParameter('customFormat', i) as string;
|
|
|
|
responseData.push({ [outputFieldName]: momentDate.format(customFormat) });
|
|
|
|
} else {
|
|
|
|
responseData.push({ [outputFieldName]: momentDate.format(format) });
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'Invalid date format');
|
|
|
|
}
|
2023-04-04 08:08:49 -07:00
|
|
|
}
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
|
|
|
{
|
|
|
|
itemData: { item: i },
|
|
|
|
},
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
|
|
|
}
|
|
|
|
return this.prepareOutputData(returnData);
|
|
|
|
}
|
2023-04-04 05:45:06 -07:00
|
|
|
}
|