Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3535 | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

export const getYears = () => {
  const date = new Date();
  const currentYear = date.getFullYear();
  let years = [];
  for (let index = currentYear; index > currentYear - 100; index--) {
    years = [...years, index];
  }
  return years;
};

export const getMonths = () => {
  const months = Array.from({ length: 12 }, (item, i) => {
    return new Date(0, i).toLocaleString('es-ES', { month: 'long' });
  });

  return months;
};

export function getTimeDiff(segundos) {
  const currentDate = new Date();
  const futureDate = new Date(currentDate.getTime() + segundos * 1000);
  const diff = futureDate - currentDate;

  const days = Math.floor(diff / (1000 * 60 * 60 * 24));
  const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));

  return `${addZero(days)}d ${addZero(hours)}h ${addZero(minutes)}m`;
}

function addZero(unit) {
  return String(unit).padStart(2, '0');
}

export const getMonthName = (monthNumber) => {
  const date = new Date();
  date.setMonth(monthNumber - 1);

  const month = date.toLocaleString('es-ES', { month: 'long' });

  return month;
};

const DATE_UNITS = {
  year: 31557600,
  month: 2628000,
  day: 86000,
  hour: 3600,
  minute: 60,
  second: 1
};

const rft = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });

export const getRelativeTime = (time) => {
  const started = new Date(time + 1000).getTime();
  const now = new Date().getTime();

  const elapsed = (started - now) / 1000;

  for (const unit in DATE_UNITS) {
    const absoluteElapsed = Math.abs(elapsed);

    if (absoluteElapsed > DATE_UNITS[unit] || unit === 'second') {
      return rft.format(Math.floor(elapsed / DATE_UNITS[unit]), unit);
    }
  }

  return '';
};

export const formatDate = (date) => {
  const dateObj = new Date(date);

  if (isNaN(dateObj)) {
    return date;
  }

  return new Intl.DateTimeFormat('es', {
    dateStyle: 'medium',
    timeStyle: 'short',
    timeZone: 'UTC',
    hour12: true
  }).format(dateObj);
};