javascript javascript从毫秒开始解析时间(分钟:秒)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5588465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript parse time (minutes:seconds) from milliseconds
提问by PeeHaa
How to parse a given amount of milliseconds (e.g. 125230.41294642858
) into a time format like: minutes:seconds
?
如何解析毫秒(如给定的量125230.41294642858
)到像一个时间格式:minutes:seconds
?
回答by Mark Kahn
var ms = 125230.41294642858,
min = 0|(ms/1000/60),
sec = 0|(ms/1000) % 60;
alert(min + ':' + sec);
回答by JaredPar
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
回答by Anatoly
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'
回答by Robin Wieruch
Even though moment.js does not provide such functionality, if you come here and you are already using moment.js, try this:
即使moment.js 没有提供这样的功能,如果你来到这里并且你已经在使用moment.js,试试这个:
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.
此解决办法的时刻,这是引入了问题。