20 lines
522 B
TypeScript
20 lines
522 B
TypeScript
import { DateTime, TimeZone } from 'timezonecomplete';
|
|
|
|
export class TimeConverter {
|
|
static toUtcDatetime = (date: Date, time: string, timezone: string): Date => {
|
|
try {
|
|
if (!date || !time || !timezone) throw new Error();
|
|
return new Date(
|
|
new DateTime(
|
|
`${date.toISOString().split('T')[0]}T${time}:00`,
|
|
TimeZone.zone(timezone, false),
|
|
)
|
|
.convert(TimeZone.zone('UTC'))
|
|
.toIsoString(),
|
|
);
|
|
} catch (e) {
|
|
return undefined;
|
|
}
|
|
};
|
|
}
|