28 lines
896 B
TypeScript
28 lines
896 B
TypeScript
export const SERVER_TIME_ZONE = process.env.NEXT_PUBLIC_SERVER_TIME_ZONE ?? "Europe/Madrid";
|
|
|
|
export function getDateKeyInTimeZone(date = new Date(), timeZone = SERVER_TIME_ZONE): string {
|
|
const formatter = new Intl.DateTimeFormat("en-CA", {
|
|
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) {
|
|
return date.toISOString().slice(0, 10);
|
|
}
|
|
|
|
return `${year}-${month}-${day}`;
|
|
}
|
|
|
|
export function formatElapsed(seconds: number): string {
|
|
const minutes = Math.floor(seconds / 60);
|
|
const remainder = seconds % 60;
|
|
return `${minutes}:${remainder.toString().padStart(2, "0")}`;
|
|
}
|