简体   繁体   中英

how to calculate the amount for total hours in javascript

i need to calculate the work in the hourly base, for one hour I spent 100 Rupee means, my total amount will 100 Like that i need to calculate the hours to amount in javascript. example code

var timeSpent = 1:25:58, //1:26
    amountPerhours = 100;

how to calculate the total amount based on working hours in javascript

You could get first the decimal representation of the time and multiply it then with the hour factor. Later you might add some fixing.

 var timeSpent = '1:25:58', amountPerHour = 100, time = timeSpent .split(':') .reduce((r, a, i) => r + a * Math.pow(60, -i), 0), result = time * amountPerHour; console.log('time', time); console.log('amount', result.toFixed(2)); 

Try to split your input variables:

var hours = 3;
var minutes = 25;
var seconds = 58;
var amount = 100;

var result = hours*amount + minutes*amount/60 + seconds*amount/3600;

Try this:

 function getAmount(timeSpent) { var amountPerhour = 100; var time = timeSpent.split(":"); var hours = time[0]; var mins = time[1]; var secs = time[2]; if (secs != undefined && secs > 29) { mins++; } var amount = (+hours + +mins/60) * amountPerhour; return parseFloat(amount).toFixed(2); } console.log(getAmount("1:25:58")); 

The amount can easily be calculated in one line. Refer to the below snippet.

 var time = "1:25:30"; var amount = time.split(":") .map((a,b) => a/(60**b)) .reduce((a,b) => a+b) .toFixed(2) * 100; console.log(amount); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM