hub2wp-repo-public/public/js/utils/time.js
2025-02-14 03:33:21 +01:00

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`;
};