mirror of
https://gh.wpcy.net/https://github.com/WP-Autoplugin/hub2wp-repo-public.git
synced 2026-04-25 17:03:14 +08:00
16 lines
629 B
JavaScript
16 lines
629 B
JavaScript
export const formatTimeAgo = (dateString) => {
|
|
const date = new Date(dateString);
|
|
const now = new Date();
|
|
const diff = now - date;
|
|
const minutes = Math.floor(diff / 60000);
|
|
const hours = Math.floor(minutes / 60);
|
|
const days = Math.floor(hours / 24);
|
|
const months = Math.floor(days / 30);
|
|
const years = Math.floor(days / 365);
|
|
|
|
if (minutes < 60) return `${minutes} minutes ago`;
|
|
if (hours < 24) return `${hours} hours ago`;
|
|
if (days < 30) return `${days} days ago`;
|
|
if (months < 12) return `${months} ${months === 1 ? 'month' : 'months'} ago`;
|
|
return `${years} ${years === 1 ? 'year' : 'years'} ago`;
|
|
};
|