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) {
|
2023-04-06 08:15:49 -07:00
|
|
|
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') {
|
2023-04-06 08:15:49 -07:00
|
|
|
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;
|
|
|
|
}
|