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

前端 未结 28 2226
自闭症患者
自闭症患者 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条回答
  •  Happy的楠姐
    2020-11-27 05:20

    const duration = moment.duration(62, 'hours');
    const n = 24 * 60 * 60 * 1000;
    const days = Math.floor(duration / n);
    const str = moment.utc(duration % n).format('H [h] mm [min] ss [s]');
    console.log(`${days > 0 ? `${days} ${days == 1 ? 'day' : 'days'} ` : ''}${str}`);
    

    Prints:

    2 days 14 h 00 min 00 s

提交回复
热议问题