在下面的示例中,三個(gè)時(shí)間的平均值是01:42:22:566,我的問(wèn)題是如何在javascript中執(zhí)行此操作。 <form action="/action_page.php"> <label for="vOne">Value One:</label> <input type="text" id="vOne" name="vOne" value="01:01:01:100"><br><br> <label for="vTwo">Value Two:</label> <input type="text" id="vTwo" name="vTwo" value="01:30:45:700"><br><br> <label for="vThree">Value Three:</label> <input type="text" id="vThree" name="vThree" value="02:35:20:900"><br><br> <label for="sumValue">Sum Value:</label> <input type="text" id="sumValue" name="sumValue" value="05:07:07:700"><br><br> <label for="aResult">Average Result:</label> <input type="text" id="aResult" name="aResult" value="01:42:22:566"><br><br> <input type="button" value="average"> </form>
1 回答

喵喵時(shí)光機(jī)
TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊
你必須:
將字符串轉(zhuǎn)換為
Date
對(duì)象計(jì)算平均值
將值轉(zhuǎn)換為字符串
嘗試類似的方法:
const dates = [
'01:01:01:100',
'01:30:45:700',
'02:35:20:900',
].map((date) => new Date(0, 0, 0, ...date.split(':')));
const average = new Date(dates.reduce((a, b) => +b + a, 0) / dates.length);
const averageString =
average.toLocaleTimeString() + ':' + average.getMilliseconds();
console.log(averageString); // "01:42:22:567"
- 1 回答
- 0 關(guān)注
- 175 瀏覽
添加回答
舉報(bào)
0/150
提交
取消