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:
Elias Meire
2023-11-10 14:30:13 +01:00
committed by GitHub
parent 3c0734bd2d
commit d2d11e0208
3 changed files with 36 additions and 14 deletions

View File

@@ -130,7 +130,7 @@ export class DateTimeV2 implements INodeType {
const duration = this.getNodeParameter('duration', i) as number;
const outputFieldName = this.getNodeParameter('outputFieldName', i) as string;
const dateToAdd = parseDate.call(this, addToDate, workflowTimezone);
const dateToAdd = parseDate.call(this, addToDate, { timezone: workflowTimezone });
const returnedDate = dateToAdd.plus({ [timeUnit]: duration });
item.json[outputFieldName] = returnedDate.toString();
@@ -141,7 +141,7 @@ export class DateTimeV2 implements INodeType {
const duration = this.getNodeParameter('duration', i) as number;
const outputFieldName = this.getNodeParameter('outputFieldName', i) as string;
const dateToAdd = parseDate.call(this, subtractFromDate, workflowTimezone);
const dateToAdd = parseDate.call(this, subtractFromDate, { timezone: workflowTimezone });
const returnedDate = dateToAdd.minus({ [timeUnit]: duration });
item.json[outputFieldName] = returnedDate.toString();
@@ -150,14 +150,18 @@ export class DateTimeV2 implements INodeType {
const date = this.getNodeParameter('date', i) as string;
const format = this.getNodeParameter('format', i) as string;
const outputFieldName = this.getNodeParameter('outputFieldName', i) as string;
const { timezone } = this.getNodeParameter('options', i) as { timezone: boolean };
const { timezone, fromFormat } = this.getNodeParameter('options', i) as {
timezone: boolean;
fromFormat: string;
};
if (date === null || date === undefined) {
item.json[outputFieldName] = date;
} else {
const dateLuxon = timezone
? parseDate.call(this, date, workflowTimezone)
: parseDate.call(this, date);
const dateLuxon = parseDate.call(this, date, {
timezone: timezone ? workflowTimezone : undefined,
fromFormat,
});
if (format === 'custom') {
const customFormat = this.getNodeParameter('customFormat', i) as string;
item.json[outputFieldName] = dateLuxon.toFormat(customFormat);
@@ -171,7 +175,7 @@ export class DateTimeV2 implements INodeType {
const mode = this.getNodeParameter('mode', i) as string;
const outputFieldName = this.getNodeParameter('outputFieldName', i) as string;
const dateLuxon = parseDate.call(this, date, workflowTimezone);
const dateLuxon = parseDate.call(this, date, { timezone: workflowTimezone });
if (mode === 'roundDown') {
const toNearest = this.getNodeParameter('toNearest', i) as string;
@@ -193,8 +197,8 @@ export class DateTimeV2 implements INodeType {
isoString: boolean;
};
const luxonStartDate = parseDate.call(this, startDate, workflowTimezone);
const luxonEndDate = parseDate.call(this, endDate, workflowTimezone);
const luxonStartDate = parseDate.call(this, startDate, { timezone: workflowTimezone });
const luxonEndDate = parseDate.call(this, endDate, { timezone: workflowTimezone });
const duration = luxonEndDate.diff(luxonStartDate, unit);
if (isoString) {
item.json[outputFieldName] = duration.toString();
@@ -207,7 +211,7 @@ export class DateTimeV2 implements INodeType {
const outputFieldName = this.getNodeParameter('outputFieldName', i) as string;
const part = this.getNodeParameter('part', i) as keyof DateTime | 'week';
const parsedDate = parseDate.call(this, date, workflowTimezone);
const parsedDate = parseDate.call(this, date, { timezone: workflowTimezone });
const selectedPart = part === 'week' ? parsedDate.weekNumber : parsedDate.get(part);
item.json[outputFieldName] = selectedPart;
returnData.push(item);