mirror of
https://github.com/Sea-Haven-Industries/seahaven-ap.git
synced 2026-10-01 14:23:17 +00:00
42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
export function formatSlashDate(value: string | null | undefined): string {
|
|
if (!value) {
|
|
return "—";
|
|
}
|
|
const parsed = new Date(`${value.slice(0, 10)}T00:00:00`);
|
|
if (Number.isNaN(parsed.getTime())) {
|
|
return value;
|
|
}
|
|
return new Intl.DateTimeFormat("en-US", {
|
|
year: "numeric",
|
|
month: "2-digit",
|
|
day: "2-digit",
|
|
}).format(parsed);
|
|
}
|
|
|
|
export function formatRelativeFromIsoDate(
|
|
value: string | null | undefined,
|
|
now: Date = new Date(),
|
|
): string {
|
|
if (!value) {
|
|
return "";
|
|
}
|
|
const parsed = new Date(`${value.slice(0, 10)}T00:00:00`);
|
|
if (Number.isNaN(parsed.getTime())) {
|
|
return "";
|
|
}
|
|
const diffMs = now.getTime() - parsed.getTime();
|
|
const dayMs = 24 * 60 * 60 * 1000;
|
|
const days = Math.max(0, Math.floor(diffMs / dayMs));
|
|
if (days < 1) {
|
|
return "today";
|
|
}
|
|
if (days < 30) {
|
|
return days === 1 ? "1 day ago" : `${days} days ago`;
|
|
}
|
|
const months = Math.floor(days / 30);
|
|
if (months < 12) {
|
|
return months === 1 ? "1 month ago" : `${months} months ago`;
|
|
}
|
|
const years = Math.floor(months / 12);
|
|
return years === 1 ? "1 year ago" : `${years} years ago`;
|
|
}
|