使用Javascript将毫秒转换为分钟和秒

时间:2021-09-09 21:30:55

Soundcloud's API gives the duration of it's tracks as milliseconds. JSON looks like this:

Soundcloud的API以毫秒为单位给出其轨迹的持续时间。 JSON看起来像这样:

"duration": 298999

I've tried many functions I found on here to no avail. I'm just looking for something to convert that number to something like looks like this:

我尝试过很多我在这里找到的功能都无济于事。我只是在寻找能够将这个数字转换成如下所示的东西:

4:59

Here's one that got close, but doesn't work. It doesn't stop the seconds at 60. It goes all the way to 99 which makes no sense. Try entering "187810" as a value of ms, for example.

这是一个接近但不起作用的。它并没有停止在60秒。它一直到99没有任何意义。例如,尝试输入“187810”作为ms的值。

var ms = 298999,
min = Math.floor((ms/1000/60) << 0),
sec = Math.floor((ms/1000) % 60);

console.log(min + ':' + sec);

Thanks for your help!

谢谢你的帮助!

If you could add in support for hours, too, I would be grateful.

如果你也可以增加数小时的支持,我将不胜感激。

5 个解决方案

#1


86  

function millisToMinutesAndSeconds(millis) {
  var minutes = Math.floor(millis / 60000);
  var seconds = ((millis % 60000) / 1000).toFixed(0);
  return minutes + ":" + (seconds < 10 ? '0' : '') + seconds;
}

millisToMinutesAndSeconds(298999); // "4:59"
millisToMinutesAndSeconds(60999);  // "1:01"

As User HelpingHand pointed in the comments the return statement should be

用户HelpingHand在注释中指出return语句应该是

return (seconds == 60 ? (minutes+1) + ":00" : minutes + ":" + (seconds < 10 ? "0" : "") + seconds);

return(秒= = 60?(分钟+ 1)+“:00”:分钟+“:”+(秒<10?“0”:“”)+秒);

#2


8  

var ms = 298999;
ms = 1000*Math.round(ms/1000); // round to nearest second
var d = new Date(ms);
console.log( d.getUTCMinutes() + ':' + d.getUTCSeconds() ); // "4:59"

Also if you want the hours, use d.getUTCHours().

此外,如果您想要小时,请使用d.getUTCHours()。

#3


1  

There is probably a better way to do this, but it gets the job done:

可能有更好的方法来做到这一点,但它完成了工作:

var ms = 298999;
var min = ms / 1000 / 60;
var r = min % 1;
var sec = Math.floor(r * 60);
if (sec < 10) {
    sec = '0'+sec;
}
min = Math.floor(min);
console.log(min+':'+sec);

Not sure why you have the << operator in your minutes line, I don't think it's needed just floor the minutes before you display.

不知道你为什么在你的分钟线上有< <操作符,我不认为在显示之前只需要几分钟。< p>

Getting the remainder of the minutes with % gives you the percentage of seconds elapsed in that minute, so multiplying it by 60 gives you the amount of seconds and flooring it makes it more fit for display although you could also get sub-second precision if you want.

使用%获得剩余的分钟数,可以得到该分钟内经过的秒数百分比,因此将它乘以60可以得到秒数和地板数,使其更适合显示,尽管如果你还可以获得亚秒级精度想。

If seconds are less than 10 you want to display them with a leading zero.

如果秒小于10,则要显示前导零。

#4


1  

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

事件虽然,oment.js不提供这样的功能,如果你来到这里并且你已经在使用moment.js,试试这个:

function formatDuration(ms) {
  var duration = moment.duration(ms);
  return Math.floor(duration.asHours()) + moment.utc(duration.asMilliseconds()).format(":mm:ss");
}

You will get something like x:xx:xx.

您将获得类似x:xx:xx的内容。

In the case you may want to skip the hour, when the duration is only < 60minutes.

在这种情况下,您可能希望跳过小时,此时持续时间仅为<60分钟。

function formatDuration(ms) {
  var duration = moment.duration(ms);
  if (duration.asHours() > 1) {
    return Math.floor(duration.asHours()) + moment.utc(duration.asMilliseconds()).format(":mm:ss");
  } else {
    return moment.utc(duration.asMilliseconds()).format("mm:ss");
  }
}

This workaround in moment was introduced in this Issue.

此问题中介绍了此解决方法。

#5


-2  

this code will do a better job if you want to show hours, and centiseconds or miliseconds after seconds like 1:02:32.21 and if used in a cell phone the timer will show correct timing even after screen lock.

如果你想显示小时数,这个代码会做得更好,如果你想在1:02:32.21之后显示几秒或几毫秒,如果在手机中使用,即使在屏幕锁定之后,计时器也会显示正确的时间。

<div id="timer" style="font-family:monospace;">00:00<small>.00</small></div>

<script>
var d = new Date();
var n = d.getTime();
var startTime = n;

var tm=0;
function updateTimer(){
  d = new Date();
  n = d.getTime();
  var currentTime = n;  
  tm = (currentTime-startTime);
  
  //tm +=1; 
  // si el timer cuenta en centesimas de segundo
  //tm = tm*10;
  
  var hours = Math.floor(tm / 1000 / 60 / 60);
  var minutes = Math.floor(tm / 60000) % 60;
  var seconds =  ((tm / 1000) % 60);
  // saca los decimales ej 2 d{0,2}
  var seconds = seconds.toString().match(/^-?\d+(?:\.\d{0,-1})?/)[0];
  var miliseconds = ("00" + tm).slice(-3);
  var centiseconds;

  
  // si el timer cuenta en centesimas de segundo
  //tm = tm/10;


  centiseconds = miliseconds/10;
  centiseconds = (centiseconds).toString().match(/^-?\d+(?:\.\d{0,-1})?/)[0];

  minutes = (minutes < 10 ? '0' : '') + minutes;
  seconds = (seconds < 10 ? '0' : '') + seconds;
  centiseconds = (centiseconds < 10 ? '0' : '') + centiseconds;
  hours = hours + (hours > 0 ? ':' : '');
  if (hours==0){
    hours='';
  }

  document.getElementById("timer").innerHTML = hours + minutes + ':' + seconds + '<small>.' + centiseconds + '</small>';
}

var timerInterval = setInterval(updateTimer, 10);
// clearInterval(timerInterval);
</script>

#1


86  

function millisToMinutesAndSeconds(millis) {
  var minutes = Math.floor(millis / 60000);
  var seconds = ((millis % 60000) / 1000).toFixed(0);
  return minutes + ":" + (seconds < 10 ? '0' : '') + seconds;
}

millisToMinutesAndSeconds(298999); // "4:59"
millisToMinutesAndSeconds(60999);  // "1:01"

As User HelpingHand pointed in the comments the return statement should be

用户HelpingHand在注释中指出return语句应该是

return (seconds == 60 ? (minutes+1) + ":00" : minutes + ":" + (seconds < 10 ? "0" : "") + seconds);

return(秒= = 60?(分钟+ 1)+“:00”:分钟+“:”+(秒<10?“0”:“”)+秒);

#2


8  

var ms = 298999;
ms = 1000*Math.round(ms/1000); // round to nearest second
var d = new Date(ms);
console.log( d.getUTCMinutes() + ':' + d.getUTCSeconds() ); // "4:59"

Also if you want the hours, use d.getUTCHours().

此外,如果您想要小时,请使用d.getUTCHours()。

#3


1  

There is probably a better way to do this, but it gets the job done:

可能有更好的方法来做到这一点,但它完成了工作:

var ms = 298999;
var min = ms / 1000 / 60;
var r = min % 1;
var sec = Math.floor(r * 60);
if (sec < 10) {
    sec = '0'+sec;
}
min = Math.floor(min);
console.log(min+':'+sec);

Not sure why you have the << operator in your minutes line, I don't think it's needed just floor the minutes before you display.

不知道你为什么在你的分钟线上有< <操作符,我不认为在显示之前只需要几分钟。< p>

Getting the remainder of the minutes with % gives you the percentage of seconds elapsed in that minute, so multiplying it by 60 gives you the amount of seconds and flooring it makes it more fit for display although you could also get sub-second precision if you want.

使用%获得剩余的分钟数,可以得到该分钟内经过的秒数百分比,因此将它乘以60可以得到秒数和地板数,使其更适合显示,尽管如果你还可以获得亚秒级精度想。

If seconds are less than 10 you want to display them with a leading zero.

如果秒小于10,则要显示前导零。

#4


1  

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

事件虽然,oment.js不提供这样的功能,如果你来到这里并且你已经在使用moment.js,试试这个:

function formatDuration(ms) {
  var duration = moment.duration(ms);
  return Math.floor(duration.asHours()) + moment.utc(duration.asMilliseconds()).format(":mm:ss");
}

You will get something like x:xx:xx.

您将获得类似x:xx:xx的内容。

In the case you may want to skip the hour, when the duration is only < 60minutes.

在这种情况下,您可能希望跳过小时,此时持续时间仅为<60分钟。

function formatDuration(ms) {
  var duration = moment.duration(ms);
  if (duration.asHours() > 1) {
    return Math.floor(duration.asHours()) + moment.utc(duration.asMilliseconds()).format(":mm:ss");
  } else {
    return moment.utc(duration.asMilliseconds()).format("mm:ss");
  }
}

This workaround in moment was introduced in this Issue.

此问题中介绍了此解决方法。

#5


-2  

this code will do a better job if you want to show hours, and centiseconds or miliseconds after seconds like 1:02:32.21 and if used in a cell phone the timer will show correct timing even after screen lock.

如果你想显示小时数,这个代码会做得更好,如果你想在1:02:32.21之后显示几秒或几毫秒,如果在手机中使用,即使在屏幕锁定之后,计时器也会显示正确的时间。

<div id="timer" style="font-family:monospace;">00:00<small>.00</small></div>

<script>
var d = new Date();
var n = d.getTime();
var startTime = n;

var tm=0;
function updateTimer(){
  d = new Date();
  n = d.getTime();
  var currentTime = n;  
  tm = (currentTime-startTime);
  
  //tm +=1; 
  // si el timer cuenta en centesimas de segundo
  //tm = tm*10;
  
  var hours = Math.floor(tm / 1000 / 60 / 60);
  var minutes = Math.floor(tm / 60000) % 60;
  var seconds =  ((tm / 1000) % 60);
  // saca los decimales ej 2 d{0,2}
  var seconds = seconds.toString().match(/^-?\d+(?:\.\d{0,-1})?/)[0];
  var miliseconds = ("00" + tm).slice(-3);
  var centiseconds;

  
  // si el timer cuenta en centesimas de segundo
  //tm = tm/10;


  centiseconds = miliseconds/10;
  centiseconds = (centiseconds).toString().match(/^-?\d+(?:\.\d{0,-1})?/)[0];

  minutes = (minutes < 10 ? '0' : '') + minutes;
  seconds = (seconds < 10 ? '0' : '') + seconds;
  centiseconds = (centiseconds < 10 ? '0' : '') + centiseconds;
  hours = hours + (hours > 0 ? ':' : '');
  if (hours==0){
    hours='';
  }

  document.getElementById("timer").innerHTML = hours + minutes + ':' + seconds + '<small>.' + centiseconds + '</small>';
}

var timerInterval = setInterval(updateTimer, 10);
// clearInterval(timerInterval);
</script>