Feat format date operation

This commit is contained in:
agobrech 2023-04-05 16:47:44 +02:00
parent 20dd9e0e22
commit 38febb87c4
2 changed files with 124 additions and 0 deletions

View file

@ -14,6 +14,7 @@ import moment from 'moment-timezone';
import { CurrentDateDescription } from './CurrentDateDescription';
import { AddToDateDescription } from './AddToDateDescription';
import { SubtractFromDateDescription } from './SubtractFromDateDescription';
import { FormatDateDescription } from './FormatDateDescription';
export class DateTimeV2 implements INodeType {
description: INodeTypeDescription;
@ -69,6 +70,7 @@ export class DateTimeV2 implements INodeType {
...CurrentDateDescription,
...AddToDateDescription,
...SubtractFromDateDescription,
...FormatDateDescription,
],
};
}
@ -138,6 +140,21 @@ export class DateTimeV2 implements INodeType {
} catch {
throw new NodeOperationError(this.getNode(), 'Invalid date format');
}
} 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');
}
}
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(responseData as IDataObject[]),

View file

@ -0,0 +1,107 @@
import type { INodeProperties } from 'n8n-workflow';
export const FormatDateDescription: INodeProperties[] = [
{
displayName:
"You can also do this using an expression, e.g. <code>{{your_date.format('yyyy-MM-dd')}}</code>. <a target='_blank' href='https://docs.n8n.io/code-examples/expressions/luxon/'>More info</a>",
name: 'notice',
type: 'notice',
default: '',
displayOptions: {
show: {
operation: ['formatDate'],
},
},
},
{
displayName: 'Date',
name: 'date',
type: 'string',
description: 'The date that you want to format',
default: '',
displayOptions: {
show: {
operation: ['formatDate'],
},
},
},
{
displayName: 'Format',
name: 'Format',
type: 'options',
displayOptions: {
show: {
action: ['format'],
custom: [false],
},
},
// eslint-disable-next-line n8n-nodes-base/node-param-options-type-unsorted-items
options: [
{
name: 'Custom Format',
value: 'custom',
},
{
name: 'MM/DD/YYYY',
value: 'MM/DD/YYYY',
description: 'Example: 09/04/1986',
},
{
name: 'YYYY/MM/DD',
value: 'YYYY/MM/DD',
description: 'Example: 1986/04/09',
},
{
name: 'MMMM DD YYYY',
value: 'MMMM DD YYYY',
description: 'Example: April 09 1986',
},
{
name: 'MM-DD-YYYY',
value: 'MM-DD-YYYY',
description: 'Example: 09-04-1986',
},
{
name: 'YYYY-MM-DD',
value: 'YYYY-MM-DD',
description: 'Example: 1986-04-09',
},
{
name: 'Unix Timestamp',
value: 'X',
description: 'Example: 513388800.879',
},
{
name: 'Unix Ms Timestamp',
value: 'x',
description: 'Example: 513388800',
},
],
default: 'MM/DD/YYYY',
description: 'The format to convert the date to',
},
{
displayName: 'Custom Format',
name: 'customFormat',
type: 'string',
displayOptions: {
show: {
format: ['custom'],
},
},
hint: 'List of special tokens <a target="_blank" href="https://moment.github.io/luxon/#/formatting?id=table-of-tokens">More info</a>',
default: 'yyyy-MM-dd',
},
{
displayName: 'Output Field Name',
name: 'outputFieldName',
type: 'string',
default: 'formattedDate',
description: 'Name of the field to put the output in',
displayOptions: {
show: {
operation: ['formatDate'],
},
},
},
];