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

51 lines
1.5 KiB
TypeScript
Raw Normal View History

feat(Date & Time Node): Overhaul of the node (#5904) * Setup versionized node * Fix node naming * Set all possible actions * Add Current Date operation * Add timezone to current date * feat add to date operator * Change output field name to camel case * Fix info box for luxons tip * Feat subtract to date operation * Feat format date operation * Fix to node field for format date * Feat rounding operation * Feat get in between date operation * Feat add extract date operation * Add generic function for parsing date * Remove moment methods from operations * Change moment to luxon for the rest of the operations * Fix Format date operation * Fix format value * Add timezone option for current date * Add tests, improve workflow settings for testing, toString the results * Change icon for V2 * Revert "Change icon for V2" This reverts commit 46b59bea2ec6dd02a22f8d07a9736b42d751d10f. * Change workflow test name * Fix ui bug for custom format * Fix default value for format operation * Fix info box for rounding operation * Change default for units for between time operation * Inprove fields and resort time units * Fix extract week number * Resolve issue with formating and timezones * Fix field name and unit order * :zap: restored removed test case, sync v1 with curent master * :zap: parseDate update to support timestamps, tests * Keep same field for substract and add time * Update unit test * Improve visibility, add iso to string option * Update option naming --------- Co-authored-by: Michael Kret <michael.k@radency.com>
2023-05-08 08:34:14 -07:00
import { DateTime } from 'luxon';
import moment from 'moment';
import type { IExecuteFunctions } from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
export function parseDate(
this: IExecuteFunctions,
date: string | number | DateTime,
timezone?: string,
) {
let parsedDate;
if (date instanceof DateTime) {
parsedDate = date;
} else {
// Check if the input is a number
if (!Number.isNaN(Number(date))) {
//input is a number, convert to number in case it is a string formatted number
date = Number(date);
// check if the number is a timestamp in float format and convert to integer
if (!Number.isInteger(date)) {
date = date * 1000;
}
}
if (Number.isInteger(date)) {
const timestampLengthInMilliseconds1990 = 12;
// check if the number is a timestamp in seconds or milliseconds and create a moment object accordingly
if (date.toString().length < timestampLengthInMilliseconds1990) {
parsedDate = DateTime.fromSeconds(date as number);
} else {
parsedDate = DateTime.fromMillis(date as number);
}
} else {
if (!timezone && (date as string).includes('+')) {
const offset = (date as string).split('+')[1].slice(0, 2) as unknown as number;
timezone = `Etc/GMT-${offset * 1}`;
}
parsedDate = DateTime.fromISO(moment(date).toISOString());
}
parsedDate = parsedDate.setZone(timezone || 'Etc/UTC');
if (parsedDate.invalidReason === 'unparsable') {
throw new NodeOperationError(this.getNode(), 'Invalid date format');
}
}
return parsedDate;
}