javascript parse time (minutes:seconds) from milli

2019-01-25 07:46发布

How to parse a given amount of milliseconds (e.g. 125230.41294642858) into a time format like: minutes:seconds?

4条回答
干净又极端
2楼-- · 2019-01-25 08:26

Even though moment.js does not provide such functionality, if you come here and you are already using moment.js, try this:

function getFormattedMs(ms) {
  var duration = moment.duration(ms);
  return moment.utc(duration.asMilliseconds()).format("mm:ss");
}

This workaround in moment was introduced in this Issue.

查看更多
3楼-- · 2019-01-25 08:28
Number.prototype.toTime = function(){
     var self = this/1000;
     var min = (self) << 0;
     var sec = (self*60) % 60;
     if (sec == 0) sec = '00';

     return min + ':' + sec
};


var ms = (new Number('250')).toTime();
console.log(ms);
=> '0:15'

var ms = (new Number('10500')).toTime();
console.log(ms);
=> '10:30'
查看更多
Explosion°爆炸
4楼-- · 2019-01-25 08:38
var ms = 125230.41294642858,
   min = 0|(ms/1000/60),
   sec = 0|(ms/1000) % 60;

alert(min + ':' + sec);
查看更多
Deceive 欺骗
5楼-- · 2019-01-25 08:51

Try the following

var num = Number(theTextValue);
var seconds = Math.floor(num / 1000);
var minutes = Math.floor(seconds / 60);
var seconds = seconds - (minutes * 60);
var format = minutes + ':' + seconds
查看更多
登录 后发表回答