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

前端 未结 28 2126
自闭症患者
自闭症患者 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 04:54

    How about native javascript?

    var formatTime = function(integer) {
        if(integer < 10) {
            return "0" + integer; 
        } else {
            return integer;
        }
    }
    
    function getDuration(ms) {
        var s1 = Math.floor(ms/1000);
        var s2 = s1%60;
        var m1 = Math.floor(s1/60);
        var m2 = m1%60;
        var h1 = Math.floor(m1/60);
        var string = formatTime(h1) +":" + formatTime(m2) + ":" + formatTime(s2);
        return string;
    }
    
    0 讨论(0)
  • 2020-11-27 04:56
    var diff = moment(end).unix() - moment(start).unix();
    moment.utc(moment.duration(diff).asMilliseconds()).format("HH:mm:ss.SSS");
    
    0 讨论(0)
  • 2020-11-27 04:57

    You don't need .format. Use durations like this:

    const duration = moment.duration(83, 'seconds');
    console.log(duration.minutes() + ':' +duration.seconds());
    // output: 1:23
    

    I found this solution here: https://github.com/moment/moment/issues/463

    EDIT:

    And with padding for seconds, minutes and hours:

    const withPadding = (duration) => {
        if (duration.asDays() > 0) {
            return 'at least one day';
        } else {
            return [
                ('0' + duration.hours()).slice(-2),
                ('0' + duration.minutes()).slice(-2),
                ('0' + duration.seconds()).slice(-2),
            ].join(':')
        }
    }
    
    withPadding(moment.duration(83, 'seconds'))
    // 00:01:23
    
    withPadding(moment.duration(6048000, 'seconds'))
    // at least one day
    
    0 讨论(0)
  • 2020-11-27 04:58

    My solution that does not involve any other library and it works with diff > 24h

    var momentInSeconds = moment.duration(n,'seconds')
    console.log(("0" + Math.floor(momentInSeconds.asHours())).slice(-2) + ':' + ("0" + momentInSeconds.minutes()).slice(-2) + ':' + ("0" + momentInSeconds.seconds()).slice(-2))
    
    0 讨论(0)
  • 2020-11-27 05:00

    The best scenario for my particular use case was:

    var duration = moment.duration("09:30"),
        formatted = moment.utc(duration.asMilliseconds()).format("HH:mm");
    

    This improves upon @Wilson's answer since it does not access private internal property _data.

    0 讨论(0)
  • 2020-11-27 05:01

    Use moment-duration-format.

    Client Framework (ex: React)

    import moment from 'moment';
    import momentDurationFormatSetup from 'moment-duration-format';
    momentDurationFormatSetup(moment);
    
    const breakLengthInMinutes = moment.duration(breakLengthInSeconds, 's').format('m');
    

    Server (node.js)

    const moment = require("moment-timezone");
    const momentDurationFormatSetup = require("moment-duration-format");
    
    momentDurationFormatSetup(moment);
    
    const breakLengthInMinutes = moment.duration(breakLengthInSeconds, 's').format('m');
    
    0 讨论(0)
提交回复
热议问题