How to Perform Timing Subtraction using Moment Js?

2020-07-26 09:31发布

Iam trying to perform Subtraction in Momentjs:-

// Find the duration between two dates
                var breakfast1 = moment('11:32','HH:mm');
                var lunch1 = moment('12:52','HH:mm');
                alert( moment.duration(lunch1 - breakfast1).humanize() + ' between meals' ); // 1 hours between meals 

Output is alerting Only Hours .But How can I get "01:20"as Output Instead of 1 hour Any Ideas ?

how to generate Minutes along with Hours

标签: momentjs
1条回答
甜甜的少女心
2楼-- · 2020-07-26 10:32

.humanize() will always round to a specific interval. Break your code up and manually create the display you want...

            var breakfast1 = moment('11:32','HH:mm');
            var lunch1 = moment('12:52','HH:mm');
            var dur = moment.duration(lunch1 - breakfast1);
            alert( dur.hours() + ":" + dur.minutes() + ' between meals' );
查看更多
登录 后发表回答