计算两次之间的差异并递增结果
Calculate difference between two times and increment the result
我正在尝试计算两次之间的差异并递增结果。对于这些,我尝试了下面的代码。
function toSeconds(time_str) {
// Extract hours, minutes and seconds
var parts = time_str.split(':');
// compute and return total seconds
return parts[0] * 3600 + // an hour has 3600 seconds
parts[1] * 60 + // a minute has 60 seconds
+
parts[2]; // seconds
}
var a = "12:00:00"
var b = "13:05:02"
var difference = Math.abs(toSeconds(a) - toSeconds(b));
// format time differnece
var result = [
Math.floor(difference / 3600), // an hour has 3600 seconds
Math.floor((difference % 3600) / 60), // a minute has 60 seconds
difference % 60
];
// 0 padding and concatation
result = result.map(function (v) {
return v < 10 ? '0' + v : v;
}).join(':');
alert(result);
假设开始时间 00:05 上午和结束时间 00:10 上午。如果我们在这两个时间之间计算,结果将是 5 分钟。所以我们需要增加这些,比如 00:05:00。现在开始,时间将从秒、分钟到小时递增
但如果给出 12 小时 格式来计算剩余小时数,它就可以工作。有没有可能计算两次之间的差异并增加结果时间.
这是你想要的吗?我创建了一个函数,使用 Javascript 的 setInterval 每 1 秒递增一次结果变量。您可以通过调用 clearInterval(x)
结束循环
function toSeconds(time_str) {
// Extract hours, minutes and seconds
var parts = time_str.split(':');
// compute and return total seconds
return parts[0] * 3600 + // an hour has 3600 seconds
parts[1] * 60 + // a minute has 60 seconds
+
parts[2]; // seconds
}
var a = "12:00:00"
var b = "13:05:02"
var difference = Math.abs(toSeconds(a) - toSeconds(b));
// format time differnece
var result = [
Math.floor(difference / 3600), // an hour has 3600 seconds
Math.floor((difference % 3600) / 60), // a minute has 60 seconds
difference % 60
];
// 0 padding and concatation
// result = result.map(function (v) {
// return v < 10 ? '0' + v : v;
// }).join(':');
console.log(result);
let x = setInterval(function() {
result[2] = result[2] + 1;
if(result[2]>=60){
result[2] = 0;
result[1] = result[1] + 1;
}
if(result[1]>=60){
result[1] = 0;
result[0] = result[0] +1
}
result[0] = result[0] === 24 ? 0 : result[0]
console.log(result.map(function (v) {
return v < 10 ? '0' + v : v;
}).join(':'))
}, 1000);
我正在尝试计算两次之间的差异并递增结果。对于这些,我尝试了下面的代码。
function toSeconds(time_str) {
// Extract hours, minutes and seconds
var parts = time_str.split(':');
// compute and return total seconds
return parts[0] * 3600 + // an hour has 3600 seconds
parts[1] * 60 + // a minute has 60 seconds
+
parts[2]; // seconds
}
var a = "12:00:00"
var b = "13:05:02"
var difference = Math.abs(toSeconds(a) - toSeconds(b));
// format time differnece
var result = [
Math.floor(difference / 3600), // an hour has 3600 seconds
Math.floor((difference % 3600) / 60), // a minute has 60 seconds
difference % 60
];
// 0 padding and concatation
result = result.map(function (v) {
return v < 10 ? '0' + v : v;
}).join(':');
alert(result);
假设开始时间 00:05 上午和结束时间 00:10 上午。如果我们在这两个时间之间计算,结果将是 5 分钟。所以我们需要增加这些,比如 00:05:00。现在开始,时间将从秒、分钟到小时递增
但如果给出 12 小时 格式来计算剩余小时数,它就可以工作。有没有可能计算两次之间的差异并增加结果时间.
这是你想要的吗?我创建了一个函数,使用 Javascript 的 setInterval 每 1 秒递增一次结果变量。您可以通过调用 clearInterval(x)
function toSeconds(time_str) {
// Extract hours, minutes and seconds
var parts = time_str.split(':');
// compute and return total seconds
return parts[0] * 3600 + // an hour has 3600 seconds
parts[1] * 60 + // a minute has 60 seconds
+
parts[2]; // seconds
}
var a = "12:00:00"
var b = "13:05:02"
var difference = Math.abs(toSeconds(a) - toSeconds(b));
// format time differnece
var result = [
Math.floor(difference / 3600), // an hour has 3600 seconds
Math.floor((difference % 3600) / 60), // a minute has 60 seconds
difference % 60
];
// 0 padding and concatation
// result = result.map(function (v) {
// return v < 10 ? '0' + v : v;
// }).join(':');
console.log(result);
let x = setInterval(function() {
result[2] = result[2] + 1;
if(result[2]>=60){
result[2] = 0;
result[1] = result[1] + 1;
}
if(result[1]>=60){
result[1] = 0;
result[0] = result[0] +1
}
result[0] = result[0] === 24 ? 0 : result[0]
console.log(result.map(function (v) {
return v < 10 ? '0' + v : v;
}).join(':'))
}, 1000);