All files / src/utils eventDate.ts

87.5% Statements 21/24
85% Branches 17/20
100% Functions 2/2
86.95% Lines 20/23

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 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 823x       5x   5x       5x           5x           5x 4x     1x   1x       1x           1x             1x       1x 1x                           3x           13x   8x       8x 5x     3x  
export function formatEventDateLabel(
  startTime: string,
  endTime?: string | null,
): string {
  const start = new Date(startTime);
 
  Iif (Number.isNaN(start.getTime())) {
    return 'Invalid date';
  }
 
  const startDatePart = start.toLocaleDateString([], {
    day: 'numeric',
    month: 'short',
    year: 'numeric',
  });
 
  const startTimePart = start.toLocaleTimeString([], {
    hour: '2-digit',
    minute: '2-digit',
    hour12: false,
  });
 
  if (!endTime) {
    return `${startDatePart} • ${startTimePart}`;
  }
 
  const end = new Date(endTime);
 
  Iif (Number.isNaN(end.getTime())) {
    return `${startDatePart} • ${startTimePart}`;
  }
 
  const endDatePart = end.toLocaleDateString([], {
    day: 'numeric',
    month: 'short',
    year: 'numeric',
  });
 
  const endTimePart = end.toLocaleTimeString([], {
    hour: '2-digit',
    minute: '2-digit',
    hour12: false,
  });
 
  const isSameDay =
    start.getFullYear() === end.getFullYear() &&
    start.getMonth() === end.getMonth() &&
    start.getDate() === end.getDate();
 
  Eif (isSameDay) {
    return `${startDatePart} • ${startTimePart} - ${endTimePart}`;
  }
 
  return `${startDatePart} • ${startTimePart} - ${endDatePart} • ${endTimePart}`;
}
 
/**
 * Returns the number of days remaining before an in-progress event without an
 * end date is auto-completed (at the 60-day mark), or `null` when no warning
 * should be displayed.
 *
 * A warning is shown between day 53 and day 59 (inclusive) since the event
 * started.
 */
export function getAutoCompletionDaysLeft(
  status: string,
  startTime: string,
  endTime?: string | null,
  now: Date = new Date(),
): number | null {
  if (status !== 'IN_PROGRESS' || endTime) return null;
 
  const daysSinceStart = Math.floor(
    (now.getTime() - new Date(startTime).getTime()) / (1000 * 60 * 60 * 24),
  );
 
  if (daysSinceStart >= 53 && daysSinceStart < 60) {
    return 60 - daysSinceStart;
  }
 
  return null;
}