li / frontend /src /shared /lib /format.ts
hf-deploy
Deploy Grok2API v3.0.7 to Hugging Face Space
0208fd2
Raw
History Blame Contribute Delete
989 Bytes
export function formatDateTime(value: string | null | undefined, locale: string): string {
if (!value) {
return "-";
}
const date = new Date(value);
if (Number.isNaN(date.getTime())) {
return "-";
}
return new Intl.DateTimeFormat(locale, {
dateStyle: "medium",
timeStyle: "short",
}).format(date);
}
export function formatNumber(value: number, locale: string, maximumFractionDigits = 2): string {
return new Intl.NumberFormat(locale, { maximumFractionDigits }).format(value);
}
export function formatDuration(milliseconds: number): string {
if (milliseconds < 1000) {
return `${milliseconds} ms`;
}
return `${(milliseconds / 1000).toFixed(milliseconds < 10000 ? 2 : 1)} s`;
}
export function toDateTimeLocal(value: string | null | undefined): string {
if (!value) {
return "";
}
const date = new Date(value);
const offset = date.getTimezoneOffset() * 60_000;
return new Date(date.getTime() - offset).toISOString().slice(0, 19);
}