在momentjs中将毫秒转换为分钟和秒

时间:2015-11-26 09:35:25

标签: reactjs momentjs

1.from:2380 //以毫秒为单位的时间到分钟和秒   例:   2380是0.2秒转换为00:02(分:秒)

var fromDuration = moment.duration(from, 'milliseconds');
var fromMinutes = Math.ceil(fromDuration.asMinutes());
var fromSeconds = Math.ceil(fromDuration.asSeconds());

1 个答案:

答案 0 :(得分:1)

我的解决方案是:

const millisecondsToMinutesSeconds = (ms) => {
  let duration = moment.duration(ms, 'milliseconds');
  let fromMinutes = Math.floor(duration.asMinutes());
  let fromSeconds = Math.floor(duration.asSeconds() - fromMinutes * 60);

  return Math.floor(duration.asSeconds()) >= 60 ? (fromMinutes<= 9 ? '0'+fromMinutes : fromMinutes) +':'+ (fromSeconds<= 9 ? '0'+fromSeconds : fromSeconds)
: '00:'+(fromSeconds<= 9 ? '0'+fromSeconds : fromSeconds);

};