Convert milliseconds to hours and minutes using Momentjs

前端 未结 11 2447
滥情空心
滥情空心 2020-12-09 15:00

I am new to Momentjs. I am trying to use it to convert milliseconds to hours and minutes. Below, x is milliseconds

x = 433276000
var y = moment.duration(x, \         


        
相关标签:
11条回答
  • 2020-12-09 15:24

    You can create a Moment.js date from milliseconds using moment.utc().

    var milliseconds = 1000;
    moment.utc(milliseconds).format('HH:mm');
    
    0 讨论(0)
  • 2020-12-09 15:24

    In Moment.js duration you can just use Math.trunc for hours if you are expecting it to be over 24hrs. hh:mm:ss format.

    var seconds = moment.duration(value).seconds();
    var minutes = moment.duration(value).minutes();
    var hours = Math.trunc(moment.duration(value).asHours());
    

    see it here: https://codepen.io/brickgale/pen/mWqKJv?editors=1011

    0 讨论(0)
  • 2020-12-09 15:27

    moment('2000-01-01 00:00:00').millisecond(XXXXXX).format("HH:mm:ss")

    0 讨论(0)
  • 2020-12-09 15:28

    Momentjs itself doesn't support duration, in order to do so, we need a plugin moment-duration-format

    To use this plugin follow these steps (for React-js)

    import moment from 'moment';
    import momentDurationFormatSetup from "moment-duration-format";
    
    var time = moment.duration(value,unit).format('hh:mm:ss',{trim:false})
    

    Note: I have used {trim: false} as extra parameter so that it doesn't trim out extra 0's in the beginning. You can omit it if you want "11:30" instead of "00:11:30".

    0 讨论(0)
  • 2020-12-09 15:29

    Try this:

    var x = 433276000
    var d = moment.duration(x, 'milliseconds');
    var hours = Math.floor(d.asHours());
    var mins = Math.floor(d.asMinutes()) - hours * 60;
    console.log("hours:" + hours + " mins:" + mins);
    
    0 讨论(0)
提交回复
热议问题