mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
fix(Date & Time Node): Add fromFormat option to solve ambiguous date strings (#7675)
Github issue / Community forum post (link here to close automatically): https://community.n8n.io/t/spreadsheet-date-issue/31551 --------- Co-authored-by: Michael Kret <michael.k@radency.com>
This commit is contained in:
@@ -6,15 +6,18 @@ import { NodeOperationError } from 'n8n-workflow';
|
||||
export function parseDate(
|
||||
this: IExecuteFunctions,
|
||||
date: string | number | DateTime,
|
||||
timezone?: string,
|
||||
options: Partial<{
|
||||
timezone: string;
|
||||
fromFormat: string;
|
||||
}> = {},
|
||||
) {
|
||||
let parsedDate;
|
||||
|
||||
if (date instanceof DateTime) {
|
||||
parsedDate = date;
|
||||
} else {
|
||||
// Check if the input is a number
|
||||
if (!Number.isNaN(Number(date))) {
|
||||
// Check if the input is a number, don't convert to number if fromFormat is set
|
||||
if (!Number.isNaN(Number(date)) && !options.fromFormat) {
|
||||
//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
|
||||
@@ -23,6 +26,7 @@ export function parseDate(
|
||||
}
|
||||
}
|
||||
|
||||
let timezone = options.timezone;
|
||||
if (Number.isInteger(date)) {
|
||||
const timestampLengthInMilliseconds1990 = 12;
|
||||
// check if the number is a timestamp in seconds or milliseconds and create a moment object accordingly
|
||||
@@ -37,7 +41,11 @@ export function parseDate(
|
||||
timezone = `Etc/GMT-${offset * 1}`;
|
||||
}
|
||||
|
||||
parsedDate = DateTime.fromISO(moment(date).toISOString());
|
||||
if (options.fromFormat) {
|
||||
parsedDate = DateTime.fromFormat(date as string, options.fromFormat);
|
||||
} else {
|
||||
parsedDate = DateTime.fromISO(moment(date).toISOString());
|
||||
}
|
||||
}
|
||||
|
||||
parsedDate = parsedDate.setZone(timezone || 'Etc/UTC');
|
||||
|
||||
Reference in New Issue
Block a user