0

I am trying to build a calculator for the worked hours during the week. I found a way of getting a total of worked hours, but as expected Moment.js bubbles up (for example) the 29hours and 40mins to 1day 5hrs 40mins.

How do I manipulate or stop that default behavior of Moment.js?

Here is what I tried

    const calculateTotal = function (arr) {
        let hoursInTotal = moment.duration({
            minutes: 0,
            hours: 0,
        });
        // arr is the Array I am storing the total hours of each DAY
        arr.forEach(function (el) {

            // Adding every total hours of each DAY to get the total from EVERY DAY
            hoursInTotal.add(el.hours.total);
        })
        // I am getting 35.25hrs, for example. I want to get 35(h):15(min)
        console.log(hoursInTotal.hours());
        return hoursInTotal;
    }
Berin Aptula
  • 234
  • 1
  • 12
  • `console.log(hoursInTotal.get('hours'))` do what you need? – Jhecht Oct 13 '19 at 21:23
  • @Jhecht Still exceeds if the numbers are higher than 23. If the result of hours are 28, it will simply print 4hr. – Berin Aptula Oct 13 '19 at 21:40
  • And I'm assuming you've tried the hoursInTotal.asHours()? – Jhecht Oct 13 '19 at 21:42
  • @Jhecht Yes, as it returns 28.25. Whenever there are minutes involved, I want to simply show 28h15mins, instead of 28.25 – Berin Aptula Oct 13 '19 at 21:45
  • Why not just not use a moment duration then? Pretty simple math involved to keep those two numbers as variables – Jhecht Oct 13 '19 at 21:47
  • @Jhecht It is already a duration. It just bubbles up the hours to days by default. And I am looking for a solution to fix that default bubbling. – Berin Aptula Oct 13 '19 at 21:50
  • Perhaps my words could have been clearer: just don't use a moment duration if this is an issue. It's not complicated to keep track of the total hours / minutes. – Jhecht Oct 13 '19 at 22:07
  • Possible duplication of https://stackoverflow.com/a/25150793/756514 ? – Jon B Oct 13 '19 at 22:29

0 Answers0