aboutsummaryrefslogtreecommitdiff
path: root/src/utils/time.ts
blob: 734cb00660335dfd9edb72c21e306319599e3192 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
export const prettyTime = (seconds: number): string => {
  const nSeconds = seconds % 60;
  const nMinutes = Math.floor(seconds / 60);
  const nHours = Math.floor(nMinutes / 60);

  let res = '';

  if (nHours !== 0) {
    res += `${Math.round(nHours).toString().padStart(2, '0')}:`;
  }

  res += `${Math.round(nMinutes).toString().padStart(2, '0')}:${Math.round(nSeconds).toString().padStart(2, '0')}`;

  return res;
};

export const parseTime = (str: string): number => str.split(':').reduce((acc, time) => (60 * acc) + parseInt(time, 10), 0);