How to use format() on a moment.js duration?

前端 未结 28 2287
自闭症患者
自闭症患者 2020-11-27 04:34

Is there any way I can use the moment.js format method on duration objects? I can\'t find it anywhere in the docs and it doesn\'t seen to be an attribute on du

28条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 05:15

    if you use angular add this to your filters:

    .filter('durationFormat', function () {
        return function (value) {
            var days = Math.floor(value/86400000);
            value = value%86400000;
            var hours = Math.floor(value/3600000);
            value = value%3600000;
            var minutes = Math.floor(value/60000);
            value = value%60000;
            var seconds = Math.floor(value/1000);
            return (days? days + ' days ': '') + (hours? hours + ' hours ': '') + (minutes? minutes + ' minutes ': '') + (seconds? seconds + ' seconds ': '')
        }
    })
    

    usage example

    {{diff | durationFormat}}

提交回复
热议问题