Is there a simple way to convert a decimal time (e.g. 1.074 minutes) into mm:ss format using moment.js?

前端 未结 4 1632
無奈伤痛
無奈伤痛 2020-12-16 03:26

I was wondering if there\'s a simple way, using moment.js library, to transform a decimal time interval (for example, 1.074 minutes) into its equivalent \'mm:ss\' value. I a

4条回答
  •  旧时难觅i
    2020-12-16 04:29

    maybe I am a bit late to the party, but still... my two cents:

    you can build the variable in seconds, parse it as a date, and then cut it to a string or whatever format you want to use it:

    let totalTimeInSeconds = 1.074 * 60;
    let result = new Date(null, null, null, null, null, totalTimeInSeconds);
    console.log(result.toTimeString().split(' ').[0].substring(3));
    

    and the output will be:

    01:14
    

提交回复
热议问题