Skip to content

Commit

Permalink
fix(Date & Time Node): Numbers conversions fix
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-radency authored and netroy committed May 2, 2023
1 parent 8b59564 commit e11e7cd
Show file tree
Hide file tree
Showing 3 changed files with 958 additions and 338 deletions.
32 changes: 24 additions & 8 deletions packages/nodes-base/nodes/DateTime/DateTime.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,10 @@ export class DateTime implements INodeType {
item = items[i];

if (action === 'format') {
let currentDate = this.getNodeParameter('value', i) as string;
let currentDate: string | number | LuxonDateTime = this.getNodeParameter(
'value',
i,
) as string;
const dataPropertyName = this.getNodeParameter('dataPropertyName', i);
const toFormat = this.getNodeParameter('toFormat', i) as string;
const options = this.getNodeParameter('options', i);
Expand All @@ -425,28 +428,41 @@ export class DateTime implements INodeType {
currentDate = (currentDate as unknown as LuxonDateTime).toISO();
}

// Check if the input is a number
if (!Number.isNaN(Number(currentDate))) {
//input is a number, convert to number in case it is a string
currentDate = Number(currentDate);
// check if the number is a timestamp in float format and convert to integer
if (!Number.isInteger(currentDate)) {
currentDate = currentDate * 1000;
}
}

if (currentDate === undefined) {
continue;
}
if (
options.fromFormat === undefined &&
!moment(currentDate as string | number).isValid()
) {
if (options.fromFormat === undefined && !moment(currentDate).isValid()) {
throw new NodeOperationError(
this.getNode(),
'The date input format could not be recognized. Please set the "From Format" field',
{ itemIndex: i },
);
}

if (Number.isInteger(currentDate as unknown as number)) {
newDate = moment.unix(currentDate as unknown as number);
if (Number.isInteger(currentDate)) {
const timestampLengthInMilliseconds1990 = 12;
// check if the number is a timestamp in seconds or milliseconds and create a moment object accordingly
if (currentDate.toString().length < timestampLengthInMilliseconds1990) {
newDate = moment.unix(currentDate as number);
} else {
newDate = moment(currentDate);
}
} else {
if (options.fromTimezone || options.toTimezone) {
const fromTimezone = options.fromTimezone || workflowTimezone;
if (options.fromFormat) {
newDate = moment.tz(
currentDate,
currentDate as string,
options.fromFormat as string,
fromTimezone as string,
);
Expand Down
Loading

0 comments on commit e11e7cd

Please sign in to comment.