sintonia_webapp/resources/js/helpers/TimeFormatter.ts

50 lines
No EOL
1.6 KiB
TypeScript

export function timeFormatter(time: String): string {
const timeArray = time.split(':');
const hours = timeArray[0];
const minutes = timeArray[1];
const seconds = timeArray[2].split('.')[0];
let stringTime = '';
if (parseInt(hours) > 0) {
stringTime += hours+':';
}
if (parseInt(minutes) > 0 || parseInt(hours) > 0) {
stringTime += minutes+':';
}
stringTime += seconds;
if (parseInt(stringTime) === 0) {
stringTime = 'N/A';
}
return stringTime;
}
export function formatFromSeconds(seconds: number): string {
console.log(seconds)
if (seconds.includes(':')) {
let timeArray = seconds.split(':');
seconds = (parseInt(timeArray[0])*3600)+(parseInt(timeArray[1])*60)+parseFloat(timeArray[2]);
}
let stringTime = '';
let minutes = seconds / 60;
let hours = minutes / 60;
console.log(seconds, minutes, hours)
if (parseInt(hours) > 0) {
stringTime += (hours < 10) ? '0' : '';
stringTime += parseInt(hours) + ':';
seconds = seconds - (parseInt(hours) * 3600);
console.log(stringTime)
}
if (parseInt(hours) > 0 || parseInt(minutes) > 0) {
minutes = minutes - (parseInt(hours) * 60);
stringTime += (minutes < 10) ? '0' : '';
stringTime += parseInt(minutes) + ':';
seconds = seconds - (parseInt(minutes) * 60);
seconds = seconds.toFixed(2);
console.log(stringTime)
}
stringTime += (seconds < 10) ? '0' : '';
stringTime += seconds;
console.log(stringTime)
return stringTime;
}