Skip to content

Commit 5568450

Browse files
committed
feat: add getTimeAgoString function
1 parent 8130cd9 commit 5568450

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

adminforth/spa/src/utils/utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,4 +598,22 @@ export function generateMessageHtmlForRecordChange(changedFields: Record<string,
598598
const listHtml = items ? `<ul class="mt-2 list-disc list-inside">${items}</ul>` : '';
599599
const messageHtml = `<div>${escapeHtml(t('There are unsaved changes. Are you sure you want to leave this page?'))}${listHtml}</div>`;
600600
return messageHtml;
601+
}
602+
603+
export function getTimeAgoString(date: Date): string {
604+
const now = new Date();
605+
const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1000);
606+
607+
if (diffInSeconds < 60) {
608+
return `${diffInSeconds} ${diffInSeconds === 1 ? 'second' : 'seconds'} ago`;
609+
} else if (diffInSeconds < 3600) {
610+
const minutes = Math.floor(diffInSeconds / 60);
611+
return `${minutes} ${minutes === 1 ? 'minute' : 'minutes'} ago`;
612+
} else if (diffInSeconds < 86400) {
613+
const hours = Math.floor(diffInSeconds / 3600);
614+
return `${hours} ${hours === 1 ? 'hour' : 'hours'} ago`;
615+
} else {
616+
const days = Math.floor(diffInSeconds / 86400);
617+
return `${days} ${days === 1 ? 'day' : 'days'} ago`;
618+
}
601619
}

0 commit comments

Comments
 (0)