19 lines
626 B
TypeScript
19 lines
626 B
TypeScript
export function getLocalDateForTimezone(timezone: string, date = new Date()) {
|
|
const formatter = new Intl.DateTimeFormat("en-CA", {
|
|
timeZone: timezone,
|
|
year: "numeric",
|
|
month: "2-digit",
|
|
day: "2-digit",
|
|
});
|
|
|
|
const parts = formatter.formatToParts(date);
|
|
const year = parts.find((part) => part.type === "year")?.value;
|
|
const month = parts.find((part) => part.type === "month")?.value;
|
|
const day = parts.find((part) => part.type === "day")?.value;
|
|
|
|
if (!year || !month || !day) {
|
|
throw new Error("Lokale datum voor timezone kon niet worden bepaald.");
|
|
}
|
|
|
|
return `${year}-${month}-${day}`;
|
|
}
|