在JavaScript中毫秒到时间

我有这个函数格式化几秒钟的时间

function secondsToTime(secs){ var hours = Math.floor(secs / (60 * 60)); var divisor_for_minutes = secs % (60 * 60); var minutes = Math.floor(divisor_for_minutes / 60); var divisor_for_seconds = divisor_for_minutes % 60; var seconds = Math.ceil(divisor_for_seconds); return minutes + ":" + seconds; } 

它工作的很好,但我需要一个function,把毫秒的时间,我似乎无法理解我需要做的这个函数返回这种格式的时间

 mm:ss.mill 01:28.5568 

其他答案很多不必要的地板。 如果string以毫秒为单位,则转换为h:m:s,如下所示:

 function msToTime(s) { var ms = s % 1000; s = (s - ms) / 1000; var secs = s % 60; s = (s - secs) / 60; var mins = s % 60; var hrs = (s - mins) / 60; return hrs + ':' + mins + ':' + secs + '.' + ms; } 

如果你想格式化为hh:mm:ss.sss,那么使用:

 function msToTime(s) { // Pad to 2 or 3 digits, default is 2 function pad(n, z) { z = z || 2; return ('00' + n).slice(-z); } var ms = s % 1000; s = (s - ms) / 1000; var secs = s % 60; s = (s - secs) / 60; var mins = s % 60; var hrs = (s - mins) / 60; return pad(hrs) + ':' + pad(mins) + ':' + pad(secs) + '.' + pad(ms, 3); } console.log(msToTime(55018)) 

不要重新发明轮子,这里是我最喜欢的单线解决scheme:

 /** * Convert milliseconds to time string (hh:mm:ss:mss). * * @param Number ms * * @return String */ function time(ms) { return new Date(ms).toISOString().slice(11, -1); } console.log( time(12345 * 1000) ); // "03:25:45.000" 
 function millisecondsToTime(milli) { var milliseconds = milli % 1000; var seconds = Math.floor((milli / 1000) % 60); var minutes = Math.floor((milli / (60 * 1000)) % 60); return minutes + ":" + seconds + "." + milliseconds; } 

为什么不使用这样的Date对象?

 let getTime = (milli) => { let time = new Date(milli); let hours = time.getUTCHours(); let minutes = time.getUTCMinutes(); let seconds = time.getUTCSeconds(); let milliseconds = time.getUTCMilliseconds(); return hours + ":" + minutes + ":" + seconds + ":" + milliseconds; } 

https://jsfiddle.net/4sdkpso7/6/

 function millisecondsToTime(millisecs){ var ms = Math.abs(millisecs) % 1000; var secs = (millisecs < 0 ? -1 : 1) * ((Math.abs(millisecs) - ms) / 1000); ms = '' + ms; ms = '000'.substring(ms.length) + ms; return secsToTime(secs) + '.' + ms; } 

这对我工作:

 var dtFromMillisec = new Date(secs*1000); var result = dtFromMillisec.getHours() + ":" + dtFromMillisec.getMinutes() + ":" + dtFromMillisec.getSeconds(); 

的jsfiddle

编辑RobG的解决scheme并使用JavaScript的Date()。

 function msToTime(ms) { function addZ(n) { return (n<10? '0':'') + n; } var dt = new Date(ms); var hrs = dt.getHours(); var mins = dt.getMinutes(); var secs = dt.getSeconds(); var millis = dt.getMilliseconds(); var tm = addZ(hrs) + ':' + addZ(mins) + ':' + addZ(secs) + "." + millis; return tm; } 

怎么样:

 function msToTime(ms){ var secs = Math.floor(ms / 1000); var msleft = ms % 1000; var hours = Math.floor(secs / (60 * 60)); var divisor_for_minutes = secs % (60 * 60); var minutes = Math.floor(divisor_for_minutes / 60); var divisor_for_seconds = divisor_for_minutes % 60; var seconds = Math.ceil(divisor_for_seconds); return hours + ":" + minutes + ":" + seconds + ":" + msleft; } 
 var /** * Parses time in milliseconds to time structure * @param {Number} ms * @returns {Object} timeStruct * @return {Integer} timeStruct.d days * @return {Integer} timeStruct.h hours * @return {Integer} timeStruct.m minutes * @return {Integer} timeStruct.s seconds */ millisecToTimeStruct = function (ms) { var d, h, m, s; if (isNaN(ms)) { return {}; } d = ms / (1000 * 60 * 60 * 24); h = (d - ~~d) * 24; m = (h - ~~h) * 60; s = (m - ~~m) * 60; return {d: ~~d, h: ~~h, m: ~~m, s: ~~s}; }, toFormattedStr = function(tStruct){ var res = ''; if (typeof tStruct === 'object'){ res += tStruct.m + ' min. ' + tStruct.s + ' sec.'; } return res; }; // client code: var ms = new Date().getTime(), timeStruct = millisecToTimeStruct(ms), formattedString = toFormattedStr(timeStruct); alert(formattedString); 
 var secondsToTime = function(duration) { var date = new Date(duration); return "%hours:%minutes:%seconds:%milliseconds" .replace('%hours', date.getHours()) .replace('%minutes', date.getMinutes()) .replace('%seconds', date.getSeconds()) .replace('%milliseconds', date.getMilliseconds()); } 

尝试这个function: –

 function msToTime(ms) { var d = new Date(null) d.setMilliseconds(ms) return d.toLocaleTimeString("en-US") } var ms = 4000000 alert(msToTime(ms)) 

一个可行的解决scheme,为我的情况。 它把毫秒变成hh:ss时间:

 function millisecondstotime(ms) { var x = new Date(ms); var y = x.getHours(); if (y < 10) { y = '0' + y; } var z = x.getMinutes(); if (z < 10) { z = '0' + z; } return y + ':' + z; } 

更简单的解决scheme如下:

 var d = new Date(); var n = d.getMilliseconds();