n8n/packages/nodes-base/nodes/DateTime/V2/GenericFunctions.ts

21 lines
660 B
TypeScript
Raw Normal View History

2023-04-06 03:04:32 -07:00
import { DateTime } from 'luxon';
import moment from 'moment';
2023-04-06 06:03:28 -07:00
import type { IExecuteFunctions } from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
2023-04-06 03:04:32 -07:00
2023-04-06 06:03:28 -07:00
export function parseDate(this: IExecuteFunctions, date: string | DateTime, timezone: string) {
2023-04-06 03:04:32 -07:00
let parsedDate;
if (date instanceof DateTime) {
console.log('date is a DateTime object');
2023-04-06 03:04:32 -07:00
parsedDate = date;
} else {
parsedDate = DateTime.fromISO(moment.tz(date, timezone).toISOString());
2023-04-06 06:03:28 -07:00
if (parsedDate.invalidReason === 'unparsable') {
console.log(parsedDate.invalidReason);
2023-04-06 06:03:28 -07:00
throw new NodeOperationError(this.getNode(), 'Invalid date format');
}
2023-04-06 03:04:32 -07:00
}
return parsedDate;
}