All files / munch-ease-frontend/src dateformats.ts

0% Statements 0/15
0% Branches 0/1
0% Functions 0/1
0% Lines 0/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16                               
function plural(num: number, unit: string): string {
  const whole = Math.floor(num)
  return whole + ' ' + unit + (whole === 1 ? '' : 's')
}

export function ago(date: Date): string {
  const now = new Date()
  const diff = now.getTime() - date.getTime()
  if (diff < 1000) return 'just now'
  if (diff < 60 * 1000) return plural(diff / 1000, 'second') + ' ago'
  if (diff < 60 * 60 * 1000) return plural(diff / (60 * 1000), 'minute') + ' ago'
  if (diff < 24 * 60 * 60 * 1000) return plural(diff / (60 * 60 * 1000), 'hour') + ' ago'
  if (diff < 7 * 24 * 60 * 60 * 1000) return plural(diff / (24 * 60 * 60 * 1000), 'day') + ' ago'
  return date.toLocaleDateString()
}