0

I want to convert seconds into minutes. For example 61 seconds should be displayed like 1:01 (not 1:1) I tried with parseInt(sec / 60) : sec % 60. If sec=61 it is showing like 1:1.

I have data from music API that gives me music duration in seconds and I have to convert them into minutes. I tried

{parseInt(data.duration / 60 )} : {data.duration % 60}

sometimes it is giving me 3:35, 3:24 and 3:1. I have to add 0 before 1 as well. How can I do it I also tried with

{parseInt(data.duration / 60 )} : {parseFloat(data.duration % 60).toFixed(2)}

it is showing 1:1.00 I want to display it like 1:01.

Thank you for your help in advance.

Muhsin
  • 49
  • 7
  • 1
    FYI, works fine for me... `\`${parseInt(sec / 60)}:${sec % 60}\`` though personally, I'd use `Math.floor()` instead of `parseInt()` – Phil Jan 17 '22 at 05:20
  • it means 1 minute and 10 seconds. But it is showing like 1 minutes and 1 seconds. It is automatically removing 0 from end – Muhsin Jan 17 '22 at 05:20
  • Running `parseInt(70 / 60) + ":" + 70 % 60` in the console gives me `'1:10'` – Yousaf Jan 17 '22 at 05:21
  • Actually, I have data from music API. It gives me music duration with seconds. But I have to convert it into minutes. I tried with the code that I wrote above – Muhsin Jan 17 '22 at 05:23
  • No repro ~ https://codesandbox.io/s/young-monad-ocin8?file=/src/App.tsx. 70s shows as `1 : 10`. I suspect the value is actually `61` and not `70` – Phil Jan 17 '22 at 05:38
  • Oh. Yes you are right – Muhsin Jan 17 '22 at 05:39
  • So is your real question how to display `61` as `1:01`? – Phil Jan 17 '22 at 05:41
  • Yes. I edited question – Muhsin Jan 17 '22 at 05:41
  • Does this answer your question? [How can I pad a value with leading zeros?](https://stackoverflow.com/q/1267283/283366) – Phil Jan 17 '22 at 05:43
  • Not exactly, Because it will add 0 to all my results like: 1:072 it should add 0 when float num equals to 1 digit number – Muhsin Jan 17 '22 at 05:46

5 Answers5

2

try this

const convertToTime = (time) => (time < 10 ? `0${time}` : time);

const sec = 69;
const result = `${convertToTime(parseInt(sec / 60))}:${convertToTime(sec % 60)}`;;

console.log(result);
Rahul Sharma
  • 9,534
  • 1
  • 15
  • 37
2
function parseTime(time) {
    const minutes = Math.floor(time / 60);
    const second = time % 60;
    // return `${minutes}:${second < 10 ? "0" : ""}${second}`;
    return `${minutes}:${second.toString().padStart(2, "0")}`;
}

You can use the if else statement to verify the second or use padStart to make sure the length of the second is always 2.

ace1234
  • 131
  • 7
0

Thank you all for your support. I found the answer. Thank to @ace1234

{parseInt(data.duration / 60)} : {parseFloat(data.duration % 60 ).toString().padStart(2, "0")
Muhsin
  • 49
  • 7
  • 1
    Congrats. When i finished answering your question, i realized that you had solved it while i was writing the answer – Ian Jan 17 '22 at 06:16
0

You can write a logic to handle it or use padStart

const something = 7;

const fun1 = (s) => s < 10 ? `0${s}` : s;
const fun2 = (s) => s.toString().padStart(2, '0');

console.log( fun1(something) );
console.log( fun2(something) );
Ian
  • 1,198
  • 1
  • 5
  • 15
-2

Do it like

let min = sec/60;

And display like

min: sec - (min*60)

MagnusEffect
  • 3,363
  • 1
  • 16
  • 41