Shazan February 2016

Multiple a time with a number using javascript

can someone help with my code. I would like to multiply a number such as 20 with a time e.g. 35:00. I need this to work out the total cost per employee based on how much they have worked a week. I have just multiplied the two fields containing these values to see if it works, but it doesn't. Here is the code

var total = util.printd("timeDiff") * ("Total_Cost");
this.getField("Total_Cost").value = total;

Thank you

Answers


divelner February 2016

You need to take the time and parse it.

Then you need to calculate each part seperate.

var timeArr = util.printd("timeDiff").split(':');
var total = Total_Cost;
var hours = timeArr[0] * total;
var minutes = timeArr[1] * total;
this.getField("Total_Cost").value = hours + ':' + minutes;


Nina Scholz February 2016

The time must be splitted and reassambled with the correct time value. The result is cost total.

var timeDiff = '35:20',
    costPerHour = 20,
    total;

function getDecimalTime(s) {
    var p = s.split(':');
    return +p[0] + +p[1] / 60;
}

total = getDecimalTime(timeDiff) * costPerHour;
document.write(total);

Post Status

Asked in February 2016
Viewed 2,598 times
Voted 6
Answered 2 times

Search




Leave an answer