在JavaScript中的两倍之间的差异 [英] Difference Between two times as percentage in javascript
问题描述
例如:
var timeone =01:23:00; // time elapsed
var timetwo =07:34:00; //总时间
那么我如何计算总和&消耗时间并取出百分比?
更新
尝试这个,但在控制台我看到NaN
var TimeLeft = Date.parse(TimeLft.text ))/ 100;
console.log(TimeLeft);
var Totaltime = Date.parse(DealTotalTm)/ 100;
console.log(Totaltime);
var percentChange =(TimeLeft / Totaltime)* 100;
console.log(percentChange);
由于您正在使用 / em>而不是一天中的时间,那么我建议避免使用 Date
对象。它的目的是为了不同的目的。
如果您可以保证输入在小时:分钟:秒
格式您指定的,然后简单地拆分部分,以将总秒数计算为单个整数。然后,您可以划分它们以获得百分比,如下所示:
function totalSeconds(time){
var parts = time.split(':');
返回零件[0] * 3600 +零件[1] * 60 +零件[2];
}
var timeone =01:23:00; // time elapsed
var timetwo =07:34:00; //总时间
var pct =(100 * totalSeconds(timeone)/ totalSeconds(timetwo))。toFixed(2);
我已将结果固定为两位小数,但您可以根据需要进行调整。 p>
example say :
var timeone = "01:23:00"; // time elapsed
var timetwo = "07:34:00"; // total time
then how can i calculate the difference between total & elapsed time and take out the percentage of it ?
Update
Tried this but in console I see "NaN"
var TimeLeft = Date.parse(TimeLft.text())/100;
console.log(TimeLeft);
var Totaltime = Date.parse(DealTotalTm)/100;
console.log(Totaltime);
var percentChange = (TimeLeft/Totaltime)*100;
console.log(percentChange);
Since you are working with elapsed time and not time of day, then I recommend avoiding the Date
object. It's intended for a different purpose.
If you can guarantee the inputs are in the hour:minute:second
format that you specified, then simply split the parts to calculate the total seconds as a single integer. You can then divide them to obtain the percentage, like this:
function totalSeconds(time){
var parts = time.split(':');
return parts[0] * 3600 + parts[1] * 60 + parts[2];
}
var timeone = "01:23:00"; // time elapsed
var timetwo = "07:34:00"; // total time
var pct = (100 * totalSeconds(timeone) / totalSeconds(timetwo)).toFixed(2);
I've fixed the results to two decimal places, but you can adjust if you like.
这篇关于在JavaScript中的两倍之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!