Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

272
Vistas
How can I pad a value with leading zeros when value equals to one digit number?

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.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

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);

about 4 years ago · Juan Pablo Isaza Denunciar

0

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.

about 4 years ago · Juan Pablo Isaza Denunciar

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")
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda