Files
butcem/src/shared/format.ts
T

43 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Görüntüleme biçimleri (tr-TR, Europe/Istanbul). Sunucu ve istemci aynı sonucu üretir
* çünkü yerel ayar ve saat dilimi her zaman açıkça verilir (hydration uyumsuzluğu olmaz).
*/
export const LOCALE = "tr-TR";
export const TIME_ZONE = "Europe/Istanbul";
const moneyFmt = new Intl.NumberFormat(LOCALE, { style: "currency", currency: "TRY" });
/** Kuruş cinsinden tamsayı tutarı TL olarak biçimlendirir: 125050 → "₺1.250,50". */
export function formatMoney(amountMinor: number): string {
if (!Number.isSafeInteger(amountMinor)) {
throw new TypeError(`amountMinor tamsayı olmalı: ${amountMinor}`);
}
return moneyFmt.format(amountMinor / 100);
}
const dateFmt = new Intl.DateTimeFormat(LOCALE, {
timeZone: TIME_ZONE,
day: "2-digit",
month: "2-digit",
year: "numeric",
});
const dateTimeFmt = new Intl.DateTimeFormat(LOCALE, {
timeZone: TIME_ZONE,
day: "2-digit",
month: "2-digit",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
});
/** UTC tarihi İstanbul saatine göre "dd.MM.yyyy" biçiminde gösterir. */
export function formatDate(date: Date | string): string {
return dateFmt.format(new Date(date));
}
/** UTC tarihi İstanbul saatine göre "dd.MM.yyyy HH:mm" biçiminde gösterir. */
export function formatDateTime(date: Date | string): string {
return dateTimeFmt.format(new Date(date));
}