【问题标题】:DataTables and Moment - How to add duration to get a total timeDataTables 和 Moment - 如何添加持续时间以获得总时间
【发布时间】:2020-08-16 02:08:09
【问题描述】:

我正在使用 DataTables 来收集每个人筹款所花费的时间,然后显示他们在筹集到的资金中的份额以用于一个营地。

我正在尝试使用 moment 将持续时间加在一起(持续时间可以大于 24 小时)。然后,我想通过筹集的美元金额来计算每次的分数,以计算出分散给每个人的金额。例如,总计算时长为 10 小时,总筹款为 100.00 美元,詹姆斯花了 2:30,这是 10 小时的 0.25,因此将收到 25.00 美元用于营地。

我对总小时数的计算只返回最后输入的持续时间(例如,如果我输入 1:30 和 3:00,则返回的总小时数为 3:00)。

如何正确添加持续时间? 如何将每个持续时间划分为 totalHours 以获得正确的分数? 然后如何将每个人的计算支出写入第 7 列?

我的代码是:

    var totalHours;
    // this gets each node (cell) in the final column:
    memDispTable.columns(5).nodes().to$()[0].forEach(function (item) {
        // see if the display value is a number (i.e. not blank):
        var hours = $('input', item ).val();
        console.log("hours: " + hours);
        if (moment(hours, "HH:mm").isValid()) {
            totalHours = moment.duration(moment.utc(totalHours,'HH:mm')).add(moment.duration(hours, 'HH:mm'));
            totalHours = moment.utc(totalHours.as('milliseconds')).format("HH:mm");
            console.log("totalHours: " + totalHours);
        }
    });
    $('#showDispHours').val(totalHours);

@AlwaysHelping 提供了这个解决方案,但它不会超过 24 小时:

var totalHours = 0;
    // this gets each node (cell) in the final column:
    memDispTable.columns(5).nodes().to$()[0].forEach(function (item) {
        // see if the display value is a number (i.e. not blank):
        var hours = $('input', item ).val();
        console.log("hours: " + hours);
        if (moment(hours, "HH:mm").isValid()) {
            
            var start = moment(hours, "HH:mm");
            var diff = start.diff(start.clone().startOf('day'));
            totalHours += diff;
            console.log("totalHours: " + totalHours);
            
        }
    });
    
    var duration = moment.duration(totalHours); //get total
    $("#showDispHours").text(duration.hours() + ':' + duration.minutes());

除了@AlwaydHelping 提供的出色答案之外,最后一篇来自:

How to convert time milliseconds to hours, min, sec format in JavaScript?

seconds = Math.floor((totalHours / 1000) % 60),
minutes = Math.floor((totalHours / (1000 * 60)) % 60),
hours = Math.floor((totalHours / (1000 * 60 * 60)));
$("#showDispHours").text(hours + ':' + minutes);

【问题讨论】:

  • 不用担心。删除我的工作答案(根据这个问题),因为它没有帮助你,你从来没有提到任何关于侦察员和号码30
  • 嗨总是,这是一个很好的答案,除了在测试中我发现它没有超过 24 小时。我将更新问题以明确这一点。谢谢你的帮助:-)
  • 谢谢!请尝试在添加问题之前添加所有信息,以便花时间提供有效解决方案的人可以包括您最初提到的所有场景!我也可以为从 24 小时到第二天的时间提供解决方案。让我知道。请在问题中添加它。我很乐意提供帮助:)
  • 嗨,总是,我找到了剩下的部分。请把你的答案放回去,以便我接受。
  • 我已将我的答案添加回@Glyn。非常感谢!

标签: jquery datatables momentjs


【解决方案1】:

您可以使用矩的.clone() 函数和duration() 将两次相加。使用forEach 获取HH:mm 中的时间值,然后在每个循环中将值添加到totalHours var 并使用hours()minutes() 函数显示您要添加的所有时间的总和。

现场演示:

let totalHours = 0;

let getTime = $('.fundTime') //get the el
//get the value stored as time
getTime.each(function(i, el) {
  let value = el.value //get value of i.e time
  if (moment(value, "HH:mm").isValid()) {
    let start = moment(value, 'HH:mm');
    let diff = start.diff(start.clone().startOf('day'));
    totalHours += diff;
  }
})

let duration = moment.duration(totalHours); //get total
$("#total").text(duration.hours() + ':' + duration.minutes());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-duration-format/1.3.0/moment-duration-format.min.js"></script>

<input type="text" class="fundTime" value="1:31" />
<input type="text" class="fundTime" value="3:00" />


<div id="total"></div>

【讨论】:

  • 嗨总是,快到了。问题是总小时数不能超过 24。如果我有 30 名球探轮班工作,每人 2 小时,这不能提供我需要的答案 :-)
猜你喜欢
  • 2016-02-23
  • 1970-01-01
  • 1970-01-01
  • 2022-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多