muse/src/utils/time.ts

18 lines
548 B
TypeScript
Raw Normal View History

2020-03-19 04:29:43 +01:00
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;
};
2020-03-21 02:47:04 +01:00
export const parseTime = (str: string): number => str.split(':').reduce((acc, time) => (60 * acc) + parseInt(time, 10), 0);