简体   繁体   English

如何在“ timelite.js”或“ moment.js”中转换分钟和小时的总和?

[英]How can I convert the sum of minutes, hours in the 'timelite.js' or 'moment.js'?

I have array let times = ['04:20:10', '21:15:10'] . 我有数组let times = ['04:20:10', '21:15:10'] I use method add: 我使用方法添加:
let time1 = add([times]) . let time1 = add([times]) Next I use method str: str([time1]) . 接下来,我使用方法str: str([time1]) I kept 25:35:30 . 我保持25:35:30 I would like to receive the following result: 25h 35 min 30 sec . 我希望收到以下结果: 25h 35 min 30 sec If the sum would come in at 30:00:00 I would like to receive the result 30h . 如果总和是30:00:00我想在30h收到结果。 In the case of the sum of 00:30:00 I would like to receive a result of 30 min . 对于总和为00:30:00的情况,我希望收到30 min的结果。 In the case of 00:00:20 - the result 20 sec . 00:00:20的情况下-结果为20 sec How to get such a result in the 'timelite.js' or 'moment.js' library? 如何在“ timelite.js”或“ moment.js”库中获得这样的结果?

Maybe could be easier if you just format the time. 如果只是格式化时间,可能会更容易。

const formatTime = (time) => {
  const [hour, minute, sec] = time.split(':')
  return `${hour} h ${minute} min ${sec} sec`
}

const time = '25:35:30'

console.log(formatTime(time))

// 25 h 35 min 30 sec

This is just a simple example. 这只是一个简单的例子。 To achieve what you want you need to put some more logic, but yeah it's a good start. 要实现您想要的目标,您需要添加一些逻辑,但这是一个好的开始。

const convert = (time) => {
    const [hour, minute, sec] = time.split(':');

    if (hour !== "00") {
      return `${hour} h ${minute} m ${sec} s`;
    }

    if (minute !== "00") {
      return `${minute} m ${sec} s`;
    }

    return `${sec} s`;
  }

  console.log(convert('00:30:00');

//30 min 00s

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

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