说出一些事情以每小时5000的速度发生并且还有一些剩余的数量(例如,假设为2500)。
如何获得剩余的小时,分钟和秒数?
我认为这有点像:
hour = floor(remaining / perHour)
mins = floor(remaining % perHour / 60)
secs = floor(remaining % perHour % 60)
但是,使用以下方法计算备份会返回不正确的金额。
time = hour + (mins / 60) + (secs / 60 / 60)
time * perHour // 3472.2 - not 2500
时间应该更短,所以我显然在计算这个错误。如果有人要解释我哪里出错了,我将不胜感激。
编辑:我知道这不是数学SE,但数学SE应该用于高水平数学,这是下一个最佳选择。找出一种方法,但它可能不是最好的,所以我会把这个问题搁置一段时间。
hour = floor(remaining / perHour)
mins = floor(remaining / perHour * 60) - hour * 60
secs = floor(remaining / perHour * 3600) - mins * 60 - hour * 3600
答案 0 :(得分:0)
我们的问题是剩下多少时间,我们知道这个过程的速度。没有汗,我们建立一个费率(项目/分钟,项目/秒)和划分 费率剩余元素的数量。我们可以使用我们想要的任何时间单位,只要我们保持一致 - 就个人而言,我喜欢每秒的费率,因为Hz
。
演示
function getRemainingTime(remainingItems) {
const processRatePerHour = 5000;
const processRate = (processRatePerHour / 60 / 60); // 1.3888 per second
remainingSeconds = remainingItems / processRate;
return {
hours: (remainingSeconds / 3600),
minutes: (remainingSeconds / 60),
seconds: remainingSeconds
};
}
function showProgress(remainingItems) {
var time = getRemainingTime(remainingItems);
var list = document.createElement('ul');
var processEnd = moment().add(time.seconds, 's');
list.innerHTML = `
<ul>
<h3> With ${remainingItems} remaining, we will be finished ${processEnd.fromNow()}. </h3>
<li> Hours Remaining: ${time.hours} </li>
<li> Minutes Remaining: ${time.minutes} </li>
<li> Seconds Remaining: ${time.seconds} </li>
</ul>`;
document.body.appendChild(list);
}
// Let's try it out!
showProgress(4999);
showProgress(2500);
showProgress(100);
&#13;
body {
font-family: 'Helvetica', sans-serif;
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.2/moment.min.js"></script>
<h2> Please Accept My Answer v.0.2. </h2>
&#13;