Files
swingmusic-extended/src/utils/useFormatSeconds.ts
T
2022-08-04 18:43:12 +03:00

47 lines
945 B
TypeScript

/**
* Converts seconds into minutes and hours.
* @param seconds The seconds to convert
* @param long Whether to provide the time in the long format
*/
export default function formatSeconds(seconds: number, long?: boolean) {
if (seconds == undefined) {
return "00:00";
}
const date = new Date(seconds * 1000);
const hh = date.getUTCHours();
const mm = date.getUTCMinutes();
const ss = date.getUTCSeconds();
let _hh = hh < 10 ? `0${hh}` : hh;
let _mm = mm < 10 ? `0${mm}` : mm;
let _ss = ss < 10 ? `0${ss}` : ss;
if (long == true) {
if (hh === 1) {
_hh = hh + " Hour";
} else {
_hh = `${hh} Hours`;
}
if (mm === 1) {
_mm = mm + " Minute";
} else {
_mm = `${mm} Minutes`;
}
if (hh > 0) {
return `${_hh}, ${_mm}`;
} else {
return `${_mm}`;
}
}
if (hh > 0) {
return `${_hh}:${_mm}:${_ss}`;
} else {
return `${_mm}:${_ss}`;
}
}