mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
Feat add extract date operation
This commit is contained in:
parent
97d486c52f
commit
7e568bdf36
|
@ -16,8 +16,10 @@ import { AddToDateDescription } from './AddToDateDescription';
|
|||
import { SubtractFromDateDescription } from './SubtractFromDateDescription';
|
||||
import { FormatDateDescription } from './FormatDateDescription';
|
||||
import { RoundDateDescription } from './RoundDateDescription';
|
||||
import { GetTimeBetweenDates } from './GetTimeBetweenDates';
|
||||
import { DateTime, DurationUnit } from 'luxon';
|
||||
import { GetTimeBetweenDatesDescription } from './GetTimeBetweenDates';
|
||||
import type { DurationUnit } from 'luxon';
|
||||
import { DateTime } from 'luxon';
|
||||
import { ExtractDateDescription } from './ExtractDateDescription';
|
||||
|
||||
export class DateTimeV2 implements INodeType {
|
||||
description: INodeTypeDescription;
|
||||
|
@ -75,7 +77,8 @@ export class DateTimeV2 implements INodeType {
|
|||
...SubtractFromDateDescription,
|
||||
...FormatDateDescription,
|
||||
...RoundDateDescription,
|
||||
...GetTimeBetweenDates,
|
||||
...GetTimeBetweenDatesDescription,
|
||||
...ExtractDateDescription,
|
||||
],
|
||||
};
|
||||
}
|
||||
|
@ -198,6 +201,24 @@ export class DateTimeV2 implements INodeType {
|
|||
}
|
||||
const duration = luxonEndDate.diff(luxonStartDate, unit);
|
||||
responseData.push({ [outputFieldName]: duration.toObject() });
|
||||
} else if (operation === 'extractDate') {
|
||||
const date = this.getNodeParameter('date', i);
|
||||
const outputFieldName = this.getNodeParameter('outputFieldName', i) as string;
|
||||
const part = this.getNodeParameter('part', i) as keyof DateTime;
|
||||
let parsedDate;
|
||||
let selectedPart;
|
||||
try {
|
||||
console.log(date);
|
||||
parsedDate = DateTime.isDateTime(date)
|
||||
? date
|
||||
: moment.tz(date, workflowTimezone).toISOString();
|
||||
selectedPart = DateTime.isDateTime(date)
|
||||
? date.get(part)
|
||||
: DateTime.fromISO(parsedDate as string).get(part);
|
||||
} catch {
|
||||
throw new NodeOperationError(this.getNode(), 'Invalid date format');
|
||||
}
|
||||
responseData.push({ [outputFieldName]: selectedPart });
|
||||
}
|
||||
const executionData = this.helpers.constructExecutionMetaData(
|
||||
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
import type { INodeProperties } from 'n8n-workflow';
|
||||
|
||||
export const ExtractDateDescription: INodeProperties[] = [
|
||||
{
|
||||
displayName:
|
||||
'You can also do this using an expression, e.g. <code>{{ your_date.extract("month") }}}</code>. <a target="_blank" href="https://docs.n8n.io/code-examples/expressions/luxon/>More info</a>"',
|
||||
name: 'notice',
|
||||
type: 'notice',
|
||||
default: '',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: ['extractDate'],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Date',
|
||||
name: 'date',
|
||||
type: 'string',
|
||||
description: 'The date that you want to round',
|
||||
default: '',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: ['extractDate'],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Part',
|
||||
name: 'part',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Day',
|
||||
value: 'day',
|
||||
},
|
||||
{
|
||||
name: 'Hour',
|
||||
value: 'hour',
|
||||
},
|
||||
{
|
||||
name: 'Minute',
|
||||
value: 'minute',
|
||||
},
|
||||
{
|
||||
name: 'Month',
|
||||
value: 'month',
|
||||
},
|
||||
{
|
||||
name: 'Second',
|
||||
value: 'second',
|
||||
},
|
||||
{
|
||||
name: 'Week',
|
||||
value: 'week',
|
||||
},
|
||||
{
|
||||
name: 'Year',
|
||||
value: 'year',
|
||||
},
|
||||
],
|
||||
default: 'month',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: ['extractDate'],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Output Field Name',
|
||||
name: 'outputFieldName',
|
||||
type: 'string',
|
||||
default: 'datePart',
|
||||
description: 'Name of the field to put the output in',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: ['extractDate'],
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
|
@ -1,6 +1,6 @@
|
|||
import type { INodeProperties } from 'n8n-workflow';
|
||||
|
||||
export const GetTimeBetweenDates: INodeProperties[] = [
|
||||
export const GetTimeBetweenDatesDescription: INodeProperties[] = [
|
||||
{
|
||||
displayName: 'Start Date',
|
||||
name: 'startDate',
|
||||
|
|
Loading…
Reference in a new issue