summaryrefslogtreecommitdiff
path: root/packages/tweetdeck/src/lib/utils/time.ts
blob: f2802bfe128cb794d05ed77a1614a6dc577028fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
export function timeAgo(date: string | number | Date) {
  const ts = typeof date === "string" || typeof date === "number" ? new Date(date).getTime() : date.getTime();
  const diff = Date.now() - ts;
  const seconds = Math.floor(diff / 1000);
  if (seconds < 60) return `${seconds}s`;
  const minutes = Math.floor(seconds / 60);
  if (minutes < 60) return `${minutes}m`;
  const hours = Math.floor(minutes / 60);
  if (hours < 24) return `${hours}h`;
  const days = Math.floor(hours / 24);
  if (days < 7) return `${days}d`;
  const weeks = Math.floor(days / 7);
  if (weeks < 4) return `${weeks}w`;
  const months = Math.floor(days / 30);
  if (months < 12) return `${months}mo`;
  const years = Math.floor(days / 365);
  return `${years}y`;
}