forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.ts
More file actions
45 lines (39 loc) · 1.34 KB
/
Copy pathformat.ts
File metadata and controls
45 lines (39 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import i18n from '../i18n/config';
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
export const formatCurrency = (amount: number, currency: string = 'USD') => {
return new Intl.NumberFormat(i18n.language, {
style: 'currency',
currency: currency,
}).format(amount);
};
export const formatDate = (date: string | Date) => {
const d = typeof date === 'string' ? new Date(date) : date;
return new Intl.DateTimeFormat(i18n.language, {
dateStyle: 'medium',
}).format(d);
};
export const formatNumber = (num: number) => {
return new Intl.NumberFormat(i18n.language).format(num);
};
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
export function formatRelativeTime(date: Date): string {
const now = new Date();
const diffMs = now.getTime() - date.getTime();
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
if (diffDays === 0) return "Today";
if (diffDays === 1) return "Yesterday";
if (diffDays < 7) return `${diffDays}d ago`;
if (diffDays < 30) return `${Math.floor(diffDays / 7)}w ago`;
return date.toLocaleDateString("en-US", { month: "short", day: "numeric" });
}
export function getInitials(name: string): string {
return name
.split(" ")
.map((w) => w[0])
.slice(0, 2)
.join("")
.toUpperCase();
}