jquery 使用javascript的时间总和

raogr8fs  于 2022-12-18  发布在  jQuery
关注(0)|答案(8)|浏览(206)

如何在javascript中使用summtime

01:00:00
00:30:00
00:30:00

我有时间像上面我想要的总和给定的时间像

sum of above time= 02:00:00

如果我使用javascript方法setHours()、setMinutes(),这些函数将替换旧时间并显示新添加时间,例如:

1. new Date( new Date(0, 0, 0, 00, 00, 00, 0)).setMinutes(30)
2. new Date(new Date( new Date(0, 0, 0, 00, 00, 00, 0)).setMinutes(30)).setMinutes(30);


以上两个条件的结果相同,但我希望在这里00:30 + 00:30 = 01:00
.请帮忙提前致谢

axzmvihb

axzmvihb1#

一些函数可帮助您在格式化的时间长度和整数形式的秒数之间来回切换:

function timestrToSec(timestr) {
  var parts = timestr.split(":");
  return (parts[0] * 3600) +
         (parts[1] * 60) +
         (+parts[2]);
}

function pad(num) {
  if(num < 10) {
    return "0" + num;
  } else {
    return "" + num;
  }
}

function formatTime(seconds) {
  return [pad(Math.floor(seconds/3600)),
          pad(Math.floor(seconds/60)%60),
          pad(seconds%60),
          ].join(":");
}

您可以使用它们来实现您想要的:

time1 = "02:32:12";
time2 = "12:42:12";
formatTime(timestrToSec(time1) + timestrToSec(time2));
// => "15:14:24"
whlutmcx

whlutmcx2#

试试这个:

var time1 = "01:00:00";
        var time2 = "00:30:00";
        var time3 = "00:30:00";
        
        var hour=0;
        var minute=0;
        var second=0;
        
        var splitTime1= time1.split(':');
        var splitTime2= time2.split(':');
        var splitTime3= time3.split(':');
        
        hour = parseInt(splitTime1[0])+parseInt(splitTime2[0])+parseInt(splitTime3[0]);
        minute = parseInt(splitTime1[1])+parseInt(splitTime2[1])+parseInt(splitTime3[1]);
        hour = hour + minute/60;
        minute = minute%60;
        second = parseInt(splitTime1[2])+parseInt(splitTime2[2])+parseInt(splitTime3[2]);
        minute = minute + second/60;
        second = second%60;
        
        alert('sum of above time= '+hour+':'+minute+':'+second);
bihw5rsg

bihw5rsg3#

如果你想用Date对象来做这个,你可以,你只需要确保在添加的时候包含你正在改变的单位的 current 值,就像这样:

var dt = new Date(0, 0, 0, 0, 0, 0, 0);
dt.setHours(dt.getHours() + 1);      // For the 01:00
dt.setMinutes(dt.getMinutes() + 30); // For the first 00:30
dt.setMinutes(dt.getMinutes() + 30); // For the second 00:30

display("Hours: " + dt.getHours());
display("Minutes: " + dt.getMinutes());

function display(msg) {
  var p = document.createElement("p");
  p.innerHTML = String(msg);
  document.body.appendChild(p);
}

当然,从技术上讲,第一次知道getHoursgetMinutes时将返回0,但为了一致性,最好始终包含它们。

qlckcl4x

qlckcl4x4#

此解决方案非常完美:

function timestrToSec(timestr) {
  var parts = timestr.split(":");
  return (parts[0] * 3600) +
         (parts[1] * 60) +
         (+parts[2]);
}

function pad(num) {
  if(num < 10) {
    return "0" + num;
  } else {
    return "" + num;
  }
}

function formatTime(seconds) {
  return [pad(Math.floor(seconds/3600)%60),
          pad(Math.floor(seconds/60)%60),
          pad(seconds%60),
          ].join(":");
}

但是最后一个函数“formatTime”有一个小bug!

return [pad(Math.floor(seconds/3600)%60), => return [pad(Math.floor(seconds/3600)),

所以没有%60!!因为如果必须计算小时总和,它可能大于60(H)

xmakbtuz

xmakbtuz5#

我的工作是:

function padnum(n){return n<10 ? '0'+n : n}

    var time1 = "00:30";
    var time2 = "00:60";

    var minute=0;
    var second=0;

    var splitTime1= time1.split(':');
    var splitTime2= time2.split(':');

    minute = parseInt(parseInt(splitTime1[0]))+parseInt(splitTime2[0]);
    second = parseInt(parseInt(splitTime1[1]))+parseInt(splitTime2[1]);

    minute = minute + second/60;
    minute =parseInt(minute);
    second = second%60;

    minute = padnum(minute);
    second = padnum(second);        

    alert('sum of above time= '+minute+':'+second);
krugob8w

krugob8w6#

下面是一个包含错误处理的函数。
尽管这是一个很大的函数,但它会产生一个有用的一行程序。只要传递一个带有值的数组,就可以很好地运行了。

function sumMinutes(values) {

  const validate = time => {
    if (time > 59 || time < 0) {
      throw new Error(
        "Hours, minutes and seconds values have to be between 0 and 59."
      );
    }
    return time;
  };

  const seconds = values
    .map(e => validate(Number(e.split(":").reverse()[0])))
    .reduce((a, b) => a + b);

  let minutes = values
    .map(e => Number(e.split(":").reverse()[1]))
    .reduce((a, b) => a + b);

  let hours = values
    .map(e =>
      e.split(":").reverse()[2] ? Number(e.split(":").reverse()[2]) : 0
    )
    .reduce((a, b) => a + b);

  minutes *= 60;
  hours *= 3600;

  let result = new Date((hours + minutes + seconds) * 1000)
    .toISOString()
    .substr(11, 8);

  return result.split(":").reverse()[2] === "00" ? result.slice(3) : result;
}

/* examples */
const seconds = ["00:03", "00:9"];
const mins = ["01:20", "1:23"];
const hours = ["00:03:59", "02:05:01"];
const mix = ["00:04:58", "10:00"];

console.log(sumMinutes(seconds)); //'00:12'
console.log(sumMinutes(mins)); //'02:43'
console.log(sumMinutes(hours)); //'02:09:00'
console.log(sumMinutes(mix)); //'14:58'
zmeyuzjn

zmeyuzjn7#

我是这样处理的,如果你只计算小时数,函数返回的是小时和分钟数,而不是天数.

function timeToMins(time) {
    var b = time.split(':');
    return b[0] * 60 + +b[1];
};

function timeFromMins(mins) {
    function z(n) {
        if (n < 0) return ('-0' + (n).toString().slice(1));
        return (n < 10 ? '0' : '') + n;
    };

    var h = (mins / 60 | 0);
    var m = mins % 60;
    return z(h) + ':' + z(m);
};

function addTimes(time0, time1) {
    return timeFromMins(timeToMins(time0) + timeToMins(time1));
};

addTimes('02:30', '-00:30');

The output will be: 02:00

// Array [0] 01:00:00
// Array [1] 00:30:00
// Array [2] 00:30:00

let time = '01:00:00';
time = addTimes(time, '00:30:00');
time = addTimes(time, '00:30:00');

The output will be: 02:00
fcipmucu

fcipmucu8#

我只是想把事情简单化。
这是我的方法:

function sumTime(time1, time2) {
    const splitTime1= time1.split(':');
    const splitTime2= time2.split(':');

    let hour = parseInt(splitTime1[0])+parseInt(splitTime2[0]);
    let minute = parseInt(splitTime1[1])+parseInt(splitTime2[1]);
    let second = parseInt(splitTime1[2])+parseInt(splitTime2[2]);
    // Fix seconds, example: 400:60:124 -> 400:62:04
    minute = minute + (Math.floor(second/60)); // 124 / 60 = 2.07 -> +2 minutes
    second = Math.floor(second%60) // 124 % 60 = 4 -> 4 seconds
    // Fix minutes, example: 400:62:04 -> 401:02:04
    hour = hour + (Math.floor(minute/60)) // 62 / 60 = 1.03 -> +1 hour
    minute = Math.floor(minute%60) // 62 % 60 = 2 -> 2 minutes

    // Formatting fields
    hour = hour.toString().padStart(2,'0');
    minute = minute.toString().padStart(2,'0');
    second = second.toString().padStart(2,'0');

    return `${hour}:${minute}:${second}`;
}

相关问题