Adapt to date-fns-tz 3.0 changes

This commit is contained in:
Matthias 2024-04-10 07:06:57 +02:00
parent c7e32720bf
commit dbccd30ec6

View File

@ -1,12 +1,12 @@
import { parse, toDate } from 'date-fns'; import { parse, toDate } from 'date-fns';
import { format, utcToZonedTime, zonedTimeToUtc } from 'date-fns-tz'; import { format, toZonedTime, fromZonedTime } from 'date-fns-tz';
import humanizeDuration from 'humanize-duration'; import humanizeDuration from 'humanize-duration';
import { isNotUndefined } from './numberformat'; import { isNotUndefined } from './numberformat';
/** Parse date from string, returns date in UTC! */ /** Parse date from string, returns date in UTC! */
export function dateFromString(datestring: string, format: string): Date { export function dateFromString(datestring: string, format: string): Date {
const localTime = parse(datestring, format, 0); const localTime = parse(datestring, format, 0);
return zonedTimeToUtc(localTime, 'UTC'); return fromZonedTime(localTime, 'UTC');
} }
let locTimeZone = 'UTC'; let locTimeZone = 'UTC';
@ -25,7 +25,7 @@ function getTimeZone(tz?: string): string {
function formatDate(date: Date, formatPattern: string, timezone?: string): string { function formatDate(date: Date, formatPattern: string, timezone?: string): string {
const timezoneRes = getTimeZone(timezone); const timezoneRes = getTimeZone(timezone);
return format(utcToZonedTime(date, timezoneRes), formatPattern, { return format(toZonedTime(date, timezoneRes), formatPattern, {
timeZone: timezoneRes, timeZone: timezoneRes,
}); });
} }