我有时间值数组。
var time = [
"03:05:11",
"00:00:12",
"03:03:14"
]
如何添加所有数组值,以使输出为"06:08:37"
("03:05:11" + "00:00:12" + "03:03:14"
之和)。
如果有N
个数组值没有,那么我们如何在没有node.js循环的情况下这样做?
答案 0 :(得分:2)
对于moment.js(http://momentjs.com/)
来说,这听起来像是一份工作首先,我们需要使用moment.duration构造函数将时间跨度解析为可用于计算的内容,然后我们使用数组reduce来总结这些值:
<%
Dim serverVariable
'Request came via a HTTP GET.
serverVariable = Request.QueryString("localVariable")
'Request came via a HTTP POST.
serverVariable = Request.Form("localVariable")
'Request came via either a HTTP GET or HTTP POST
'Using this has it's overheads.
serverVariable = Request("localVariable")
%>
格式化为&#34; hh:mm:ss&#34;格式化moment-duration-format插件可以使用(https://github.com/jsmreese/moment-duration-format):
var moment = require('moment');
var sum = [
"03:05:11",
"00:00:12",
"03:03:14"
].map(t => moment.duration(t))
.reduce((sum, current) => sum.add(current), moment.duration());
这个答案需要循环,因为所有答案都会存在,因为数据存储在一个数组中。
Hand babelified ES5 version
console.log(sum.format("hh:mm:ss"));