22 lines
754 B
TypeScript
22 lines
754 B
TypeScript
export function getLastUpdated(date: string | Date): string {
|
|
const now = new Date();
|
|
const updated = new Date(date);
|
|
const diffMs = now.getTime() - updated.getTime();
|
|
|
|
const diffMinutes = Math.floor(diffMs / (1000 * 60));
|
|
const diffHours = Math.floor(diffMs / (1000 * 60 * 60));
|
|
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
|
|
|
|
if (diffMinutes < 1) return "baru saja";
|
|
if (diffMinutes < 60) return `${diffMinutes} menit lalu`;
|
|
if (diffHours < 24) return `${diffHours} jam lalu`;
|
|
if (diffDays < 7) return `${diffDays} hari lalu`;
|
|
|
|
// kalau sudah lebih dari seminggu, tampilkan tanggal
|
|
return updated.toLocaleDateString("id-ID", {
|
|
day: "numeric",
|
|
month: "long",
|
|
year: "numeric",
|
|
});
|
|
}
|