Skip to content

Instantly share code, notes, and snippets.

@EminQasimov
Last active April 27, 2022 11:07
Show Gist options
  • Save EminQasimov/73d05c92ebc14c21c54a7b998370cd12 to your computer and use it in GitHub Desktop.
Save EminQasimov/73d05c92ebc14c21c54a7b998370cd12 to your computer and use it in GitHub Desktop.
Calculate Total Amount with Work hours by hourly rate.
function convertDurationtoSeconds(duration) {
const [hours, minutes, seconds] = duration.split(":")
return Number(hours) * 60 * 60 + Number(minutes) * 60 + Number(seconds)
}
function secondsToHms(d) {
d = Number(d)
var h = Math.floor(d / 3600)
var m = Math.floor((d % 3600) / 60)
var s = Math.floor((d % 3600) % 60)
var hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : ""
var mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : ""
var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : ""
return hDisplay + mDisplay + sDisplay
}
// your worked hours
const times = [
"2:14:42",
"3:18:16",
"5:02:58",
"5:34:54",
"6:55:03",
"6:36:40",
"11:56:32",
"8:41:35",
"9:34:46",
"6:34:24",
"0:30:00",
"9:16:16",
"0:28:43",
"0:13:00",
"1:11:37",
"0:51:25",
"0:25:41",
"0:04:30",
"1:04:48"
]
const totalSeconds = times.reduce((acc, next) => {
return (acc += convertDurationtoSeconds(next))
}, 0)
const totalReadableHours = secondsToHms(totalSeconds)
const totalHours = totalSeconds / 3600
const hourlyWorkRate = 23
const totalMoney = (totalHours * hourlyWorkRate).toFixed(2)
console.log({
"How many total hours you worked": totalReadableHours,
"Rate USD/hour": hourlyWorkRate,
"Total Hours": totalHours.toFixed(2),
"TOTAL:": `${totalHours.toFixed(2)} Hrs * ${hourlyWorkRate} USD/hr = ${totalMoney} USD`,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment