Skip to content

Instantly share code, notes, and snippets.

@karkianish
Created July 30, 2018 03:04
Show Gist options
  • Save karkianish/e46e2acc8edfbdc3c7662e2e91b4cd73 to your computer and use it in GitHub Desktop.
Save karkianish/e46e2acc8edfbdc3c7662e2e91b4cd73 to your computer and use it in GitHub Desktop.
class TimesheetCalculator {
constructor(shiftDurations) {
this.shiftDurations = shiftDurations;
}
getCalculatedTime() {
const durationsWithMinsInDecimal = this.shiftDurations.split(' ');
const durationsWithMins = [];
for (let duration of durationsWithMinsInDecimal) {
const hourPart = duration.split('.')[0];
let minutes;
if (duration.includes('.')) {
let minsAsDecimal = duration.split('.')[1];
minutes = Math.round(Number.parseFloat('.' + minsAsDecimal) * 60);
const minPartAsString = minutes.toString();
const paddedMinPart = ('00' + minPartAsString).substring(minPartAsString.length);
durationsWithMins.push(hourPart + ':' + paddedMinPart);
} else {
durationsWithMins.push(hourPart + ':00');
}
}
return durationsWithMins
}
}
const a = new TimesheetCalculator('7.56 8.22 8.08 8.5 9.9');
const b = a.getCalculatedTime();
console.log(b);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment