使用两个不同代码计算的二冲程百分比错误



. 大家好,

我的名字是伊丽莎白,我是javascript编程的新手,所以请不要对我太苛刻。

在使用两个不同的 JavaScript 函数计算增加或减少的百分比时,我遇到了问题。问题是他们向我抛出不同的百分比,我真的不知道出了什么问题,为什么他们向我抛出不同的数字。

let timein = "00:42:02"; 
let timeoutt = "00:02:32";

有了这个功能,我敢肯定 99% 的人会给我正确的百分比,即 6%。

function timestamp_to_seconds(timestamp) {
var [hours, minutes, seconds] = timestamp.split(':').map((t) => parseInt(t, 10));
return seconds + 60 * minutes + 60 * 60 * hours;
}
var original_seconds = timestamp_to_seconds(timein);
var duration_seconds = timestamp_to_seconds(timeoutt);
var new_seconds = original_seconds + duration_seconds;
var ratio = new_seconds / original_seconds;
var numero = Math.floor(ratio * 100);
var stringNumero = numero.toString();
var cadena_final = stringNumero.slice(1, 23);

cadena_final = 6%

现在这个函数没有给我抛出正确的百分比,这将是最高的百分比,我不知道为什么。

const ONE_DAY = 8.64e7; // 86400000 millis
// let time = calculo;
let time = "00:42:02"; // the time over which you want to increase the percentage  %
let hoy = new Date();
hoy.setHours(time.split(":")[0], time.split(":")[1], time.split(":")[2]); // Establece el tiempo de lo que vamos a buscar %
let manana = new Date(Date.now() + ONE_DAY).setHours("0", "0", "0");
let porcent = ((manana - hoy) / ONE_DAY * 100).toFixed("2") + "%";
// This is to increase or decrease
const HOUR = 3.6e6; // 360000
const MINUTE = 6e4; // 60000
const SECOND = 1e3; // 1000
let incPercent = 0.06; //This would be the percentage that would increase. I've put in a 6% increase
let increased = new Date(hoy.getTime() + (incPercent * HOUR));
var fecha = increased;
var hora = fecha.getHours();
var minuto = fecha.getMinutes();
var segundo = fecha.getSeconds();
if (hora < 10) {
hora = "0" + hora;
}
if (minuto < 10) {
minuto = "0" + minuto;
}
if (segundo < 10) {
segundo = "0" + segundo;
}
var horita = hora + ":" + minuto + ":" + segundo;

堀田是 = 00:45:38,增加 6%

我设置了 06% 的增加,它将我增加到 00:45:38,这将是 00:03:36 起床时间超过 00:42:02。

因此,它错误地增加了时间百分比。

在第一个函数中,它增加了 6%,即 02:31:00 在此函数中,增加 6% 将是 00:03:36

我做错了什么,第二个函数错误地计算了百分比增加?

提前非常感谢大家,我感到我的无知,一个问候

伊丽莎白

这不是编程问题。这是逻辑。

在第一部分中,您根据"00:00:00"计算timeintimein + timeout之间的比率(以秒为单位)。

但在第二部分中,您创建了一个表示单个时刻的实例,该实例基于时间值,即自 1970 年 1 月 1 日 UTC 以来的毫秒数。来自 MDN

比较基准完全不同。

将其更改为相同的基准,这将是正确的。

let timein = "00:42:02";
let timeoutt = "00:02:32"; // timeout, a typo here?
function timestamp_to_seconds(timestamp) {
var [hours, minutes, seconds] = timestamp.split(':').map((t) => parseInt(t, 10));
return seconds + 60 * minutes + 60 * 60 * hours;
}
let start = new Date()
start.setHours(timein.split(":")[0], timein.split(":")[1], timein.split(":")[2], 0);
var duration_seconds = timestamp_to_seconds(timeoutt) * 1000;
var end = new Date(start.getTime() + duration_seconds);
var ratio = end.getTime() / start.getTime();
console.log('ratio:', ratio);
let time = "00:42:02";
let hoy = new Date();
hoy.setHours(time.split(":")[0], time.split(":")[1], time.split(":")[2], 0);
const HOUR = 3.6e6; // 360000 <-miss a zero here BTW
let increased = new Date(hoy.getTime() * ratio);
var fecha = increased;
var hora = fecha.getHours();
var minuto = fecha.getMinutes();
var segundo = fecha.getSeconds();
if (hora < 10) {
hora = "0" + hora;
}
if (minuto < 10) {
minuto = "0" + minuto;
}
if (segundo < 10) {
segundo = "0" + segundo;
}
var horita = hora + ":" + minuto + ":" + segundo;
;
(function() {
console.log('expect: 00:44:34')
console.log('result:', horita)
})();

编辑:

根据评论,这是使用增加比率的另一种方法。

而且您应该知道,增加比率是一个百分比,它显示了从timeintimein+timeout的增加比率。

所以incPercent与HOUR无关

如果像你上面所做的那样做,这意味着从timein增加到timein+timeout等于HOUR*6%,这是不正确的。

let timein = "00:42:02";
let timeoutt = "00:02:32";
function timestamp_to_seconds(timestamp) {
var [hours, minutes, seconds] = timestamp.split(':').map((t) => parseInt(t, 10));
return seconds + 60 * minutes + 60 * 60 * hours;
}
// let us skip something...
var ratio = 0.06;
console.log('ratio:', ratio);
let time = "00:42:02";
let timeInSecond = timestamp_to_seconds(time);
let increased = timeInSecond * (1 + ratio);
const Hour = 3600; // in second
const Minute = 60; // in second
var fecha = increased;
var hora = Math.floor(fecha/3600);
var minuto = Math.floor(fecha%3600/60);
var segundo = Math.floor(fecha%3600%60);
if (hora < 10) {
hora = "0" + hora;
}
if (minuto < 10) {
minuto = "0" + minuto;
}
if (segundo < 10) {
segundo = "0" + segundo;
}
var horita = hora + ":" + minuto + ":" + segundo;
;
(function() {
console.log('expect: 00:44:34')
console.log('result:', horita)
})();

PorcenNumermas ='6.00'; //Percentage to be increased in time
function Percentma() {
var checkedNew = PorcenNumermas.split(".");
uno = checkedNew[0];
dos = checkedNew[1];
//var tres = checkedNew[2];

if (dos === undefined) {
if (uno !== 10) { //If it's a 10, remove two zeros.
incPercent = 0 + "." + 10;
} else {
//   let coman = 0.0;
//  let incPercent = coman.concat(uno);
incPercent = 0 + "." + 0 + uno; // This would be the percentage
}
} else {
// let coman = 0.0;
//  let incPercent = coman.concat(uno);
incPercent = 0 + "." + 0 + uno + dos;  // This would be the percentage if 0 of the integers is not defined
}
const ONE_DAY = 8.64e7; // 86400000 milisegundos
// let time = calculo;
let time = '00:42:02'; // The time where you are looking %
let hoy = new Date();
hoy.setHours(time.split(":")[0], time.split(":")[1], time.split(":")[2]); // Sets the time of what we are going to look for %
let manana = new Date(Date.now() + ONE_DAY).setHours("0", "0", "0");
let porcent = ((manana - hoy) / ONE_DAY * 100).toFixed("2") + "%";
// 
const HOUR = 3.6e6; // 360000
const MINUTE = 6e4; // 60000
const SECOND = 1e3; // 1000
//incPercent = 0.03; // Here we select the % of increase if we hadn't written it manually in the upper variable
let increased = new Date(hoy.getTime() + (incPercent * HOUR));

var fecha = increased;
var hora = fecha.getHours();
var minuto = fecha.getMinutes();
var segundo = fecha.getSeconds();
if (hora < 10) {
hora = "0" + hora;
}
if (minuto < 10) {
minuto = "0" + minuto;
}
if (segundo < 10) {
segundo = "0" + segundo;
}
var horita = hora + ":" + minuto + ":" + segundo;

console.log('result:', horita)
}
;
Percentma();

在这里,我增加了 6% 到 00:42:02,结果他错误地扔了我 00:45:38。这就是我之前说的,你真的应该扔我00:44:34。在我忘了提及并输入我需要的百分比选项之前。提前非常感谢

最新更新