简体   繁体   English

使用JavaScript的时间总和

[英]Sum of to times using javascript

can anyone tell me how to do sum of two time using javascript (momentjs) for exemple the sum of: 谁能告诉我如何使用javascript(momentjs)来做两次求和:

2:44:56 and 2:50:56 2:44:56和2:50:56

i tried that but doesnt work: 我试过了但是没用:

2:44:56 + 2:50:56

any suggestions please?? 有什么建议吗?

Momentjs has a duration object that can be used to add or subtract two or more timespans. Momentjs有一个duration对象,可用于添加或减去两个或更多时间跨度。

const a = moment.duration('02:44:56');
const b = moment.duration('02:50:56');

const c = a.add(b);

console.log(c.hours() );
console.log(c.minutes() );
console.log(c.seconds() );

You can do it like this. 您可以这样做。 Use add method on moment object and pass your data. 在矩对象上使用add方法并传递数据。

let x = moment({  
         hours:'2', 
         minutes:'44',
         seconds:'56'})
       .add({ 
         hours:'2',
         minutes:'50', 
         seconds:'56' })
 console.log(x)

or dynamically pass data 或动态传递数据

let time = {
   hours: 2,
   minutes:44, 
   seconds: 56
}
let time2 = {
   hours: 2,
   minutes:50, 
   seconds: 56
}
let y = moment(time)
         .add(time2)
console.log(y)

One could add the seconds, then calculate the carry value and add that to the sum of minutes and so on. 可以加上秒,然后计算进位值,并将其加到分钟数总和中,依此类推。 That can be easily done with reduce: 使用reduce可以很容易地做到这一点:

function sum(date1, date2){
  date1 = date1.split(":");
  date2 = date2.split(":");
  const result = [];

  date1.reduceRight((carry,num, index) => {
    const max = [24,60,60][index];
    const add =  +date2[index];
    result.unshift( (+num+add+carry) % max );
    return Math.floor( (+num + add + carry) / max );
  },0);

 return result.join(":");
}

console.log(
  sum("2:44:56" , "2:50:56" )
);

Try it 试试吧

The code: 编码:

var t1 = moment('2:44:56', 'HH:mm:ss');
var t2 = '2:50:56';
var parsed_t2 = t2.split(':') // [2, 50, 56]

var r = t1.add({
  hours: parsed_t2[0], // 2
  minutes: parsed_t2[1], // 50
  seconds: parsed_t2[2], // 56
});

The process: 过程:

  1. Parse the string as a moment object (helping it with defining the format we're using; 将字符串解析为矩对象(帮助它定义我们正在使用的格式;
  2. Split the time we want to add to the t1 by using the split() function effectively splitting our t2 into an array where we have [hours, minutes, seconds] 通过使用split()函数来split()我们想要添加到t1的时间,可以有效地将t2分割成一个数组,其中有[hours, minutes, seconds]
  3. Add the the times together using the moments add() method. 使用moments add()方法将时间add()在一起。

Working example 工作实例

moment() function takes hours, minutes, seconds as arguments and return a moment object which has a add() method that also can take hours, minutes, seconds as arguments and return total times. moment()函数以小时,分钟,秒为参数,并返回一个带有对象add()的矩对象,该方法也可以以小时,分钟,秒为参数,并返回总时间。

Try addTimes(time1, time2) 尝试addTimes(time1,time2)

function addTimes(time1, time2) {
   let [hours1, minutes1, seconds1] = time1.split(':');
   let [hours2, minutes2, seconds2] = time2.split(':');
   return moment({ hours: hours1, minutes: minutes1, seconds: seconds1 })
       .add({ hours: hours2, minutes: minutes2, seconds: seconds2 })
       .format('h:mm:ss');
}

console.log(addTimes('2:44:56', '2:50:56'));

Good old JS solution: 好的老JS解决方案:

 var base = new Date(0); var t1 = new Date(base); var t2 = new Date(base); t1.setUTCHours(2,45,50); t2.setUTCHours(2,50,50); var t = new Date(t1.getTime() + t2.getTime() - base.getTime()); result = t.getUTCHours() + ":" + t.getUTCMinutes() +":" + t.getUTCSeconds(); console.log(result); 

Note that JS automatically converts time of the day to GMT timezone hence we need to use UTC version of time functions. 请注意,JS会自动将一天中的时间转换为GMT时区,因此我们需要使用UTC版本的时间函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM