mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
Add generic function for parsing date
This commit is contained in:
parent
7e568bdf36
commit
cd98fb5be5
|
@ -3,7 +3,7 @@ import type { INodeProperties } from 'n8n-workflow';
|
|||
export const CurrentDateDescription: INodeProperties[] = [
|
||||
{
|
||||
displayName:
|
||||
'You can also refer to the current date in n8n expressions by using <code>{{$now}}</code> or <code>{{$today}}</code>. <a target="_blank" href="https://docs.n8n.io/code-examples/expressions/luxon/>More info</a>"',
|
||||
'You can also refer to the current date in n8n expressions by using <code>{{$now}}</code> or <code>{{$today}}</code>. <a target="_blank" href="https://docs.n8n.io/code-examples/expressions/luxon/">More info</a>',
|
||||
name: 'notice',
|
||||
type: 'notice',
|
||||
default: '',
|
||||
|
|
|
@ -20,6 +20,7 @@ import { GetTimeBetweenDatesDescription } from './GetTimeBetweenDates';
|
|||
import type { DurationUnit } from 'luxon';
|
||||
import { DateTime } from 'luxon';
|
||||
import { ExtractDateDescription } from './ExtractDateDescription';
|
||||
import { parseDate } from './GenericFunctions';
|
||||
|
||||
export class DateTimeV2 implements INodeType {
|
||||
description: INodeTypeDescription;
|
||||
|
@ -202,22 +203,17 @@ 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 date = this.getNodeParameter('date', i) as string | DateTime;
|
||||
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);
|
||||
parsedDate = parseDate.call(this, date, workflowTimezone);
|
||||
} catch {
|
||||
throw new NodeOperationError(this.getNode(), 'Invalid date format');
|
||||
}
|
||||
|
||||
const selectedPart = parsedDate.get(part);
|
||||
responseData.push({ [outputFieldName]: selectedPart });
|
||||
}
|
||||
const executionData = this.helpers.constructExecutionMetaData(
|
||||
|
|
|
@ -3,7 +3,7 @@ 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>"',
|
||||
'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: '',
|
||||
|
|
14
packages/nodes-base/nodes/DateTime/V2/GenericFunctions.ts
Normal file
14
packages/nodes-base/nodes/DateTime/V2/GenericFunctions.ts
Normal file
|
@ -0,0 +1,14 @@
|
|||
import { DateTime } from 'luxon';
|
||||
import moment from 'moment';
|
||||
|
||||
export function parseDate(date: string | DateTime, timezone: string) {
|
||||
let parsedDate;
|
||||
|
||||
if (date instanceof DateTime) {
|
||||
parsedDate = date;
|
||||
console.log('here');
|
||||
} else {
|
||||
parsedDate = DateTime.fromISO(moment.tz(date, timezone).toISOString());
|
||||
}
|
||||
return parsedDate;
|
||||
}
|
Loading…
Reference in a new issue