💾 Archived View for iich.space › src › util › time.ts captured on 2022-03-01 at 16:00:14.

View Raw

More Information

⬅️ Previous capture (2021-12-03)

-=-=-=-=-=-=-

import { TranslateFunction } from '@/mission-control';

const SECONDS_IN_MINUTE = 60;
const MINUTES_IN_HOUR = 60;
const MINUTES_IN_DAY = MINUTES_IN_HOUR * 24;
const MINUTES_IN_MONTH = MINUTES_IN_DAY * 30;
const MINUTES_IN_YEAR = MINUTES_IN_MONTH * 12;

export const timeAgo = (t: TranslateFunction, date: number): string => {
  const minutes = Math.ceil((Date.now() - date) / (SECONDS_IN_MINUTE * 1000));

  if (minutes < MINUTES_IN_HOUR) {
    return t('n_minutes', minutes);
  } else if (minutes < MINUTES_IN_DAY) {
    const hours = Math.round(minutes / 60);
    return t('n_hours', hours);
  } else if (minutes < MINUTES_IN_MONTH) {
    const days = Math.round(minutes / MINUTES_IN_DAY);
    return t('n_days', days);
  } else if (minutes < MINUTES_IN_YEAR) {
    const months = Math.round(minutes / MINUTES_IN_MONTH);
    return t('n_months', months);
  }

  const years = Math.round(minutes / MINUTES_IN_YEAR);
  return t('n_years', years);
};