3

I have two input type time. 1. time of entry 2. time of exit

Example 1:

Start: 00:00 end: 01:30 result: 1.5

Example 2 :

Start: 14:00 end: 00:00 result: 10

How to create an algorithm that calculates how many employees were at work? This number will be multiplied by the rate he earns. e.g. hours * rate (1.5 * 15)

Vuzii
  • 75
  • 8

3 Answers3

1

I'd count work time in seconds cause its much clearer and fairy.

// Example data
const secondsWorked  = 4321;
const rate = 15;

const secondsInHour = 3600;

const hoursWorked = secondsWorked / secondsInHour;
// Output: 1.2002777777777778
console.log("Hours worked: ", hoursWorked);

// Output: 18.0041666666666...
const income = hoursWorked * rate;
console.log("Calculated income: ", income);
dan-maximov
  • 31
  • 1
  • 7
0

You need to change the time format.

For example:

14:30 -> 14.5

23:15 -> 23.25

And ths result is 23.25 - 14.5

0

You need separate minutes and hours and calculate difference for each. Bear mind if start minutes bigger than finish minutes you should minus 1 hour. In result you need minutes difference divide by 60 minutes.

var start = "00:56";
var finish = "05:59";

function getHours(time) {
    return time.split(":")[0];
}

function getMinutes(time) {
    return time.split(":")[1];
}

function getDifference(start, finish) {

    var hoursDifference = getHours(finish) - getHours(start);
    var minutesDifference = function () {
        if (getMinutes(finish) > getMinutes(start)) {
            return getMinutes(finish) - getMinutes(start);
        } else {
            return getMinutes(start) - getMinutes(finish);
        }
    };

    if (getMinutes(start) > getMinutes(finish)) {
        hoursDifference = hoursDifference - 1;
    }

    return hoursDifference + "." + (minutesDifference() / 60).toString().replace("0.", "");
}

var difference = getDifference(start, finish);