Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Moment.js-如何将1天2小时30分钟作为小时和分钟?_Javascript_Jquery_Momentjs - Fatal编程技术网

Javascript Moment.js-如何将1天2小时30分钟作为小时和分钟?

Javascript Moment.js-如何将1天2小时30分钟作为小时和分钟?,javascript,jquery,momentjs,Javascript,Jquery,Momentjs,我正在尝试建立一个计算一周工作时间的计算器。 我找到了一种计算总工作时间的方法,但正如预期的那样,Moment.js突然出现(例如)29小时40分钟到1天5小时40分钟 如何操作或停止矩.js的默认行为 这是我试过的 const calculateTotal = function (arr) { let hoursInTotal = moment.duration({ minutes: 0, hours: 0,

我正在尝试建立一个计算一周工作时间的计算器。 我找到了一种计算总工作时间的方法,但正如预期的那样,Moment.js突然出现(例如)29小时40分钟到1天5小时40分钟

如何操作或停止矩.js的默认行为

这是我试过的

    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;
    }

console.log(hoursInTotal.get('hours'))
做你需要做的事吗?@Jhecht如果数字大于23,则仍然超过。如果小时数的结果是28,它将只打印4小时。我假设您已经尝试了hoursInTotal.asHours()?@Jhecht Yes,因为它返回28.25。只要有分钟,我只想显示28H15分钟,而不是28.25分钟。那么为什么不使用片刻持续时间呢?将这两个数字作为变量需要相当简单的数学运算