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

165
Vistas
Why .getMinutes() output as :0 instead of :00 in JavaScript?

The .getMinutes() method only outputs one integer ‘0’ instead of two integers ‘00’. Why does it do this and is there a simpler way to fix this?

Example:

        var dateDue = new Date("Fri Apr 22 2022 18:00:00");
        dateDue.getMinutes();   //Expected output: 18:00, got 18:0 instead?

For now I have been using if statement validation to add 0 whenever the minutes .Length returns as only being one character long instead of two.

        //Minutes display validation for dateDue
        var minutesDue = dateDue.getMinutes();
        if (minutesDue.Length === 1) {
        minutesDue = parseInt(dateDue.getMinutes()) + "0";

Coding language: JavaScript (vanilla)
Skill level: Beginner
Editor: Visual Studio Code
OS: Windows 7

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

0

You can use the toLocaleTimeString method with some options for padding and even more styling. This one does AM/PM automatically for you.

const time = new Date("Fri Apr 22 2022 18:00:00").toLocaleTimeString('default', { hour: '2-digit', minute: '2-digit' });
console.log(time) // '06:00 PM'
about 4 years ago · Juan Pablo Isaza Denunciar

0

var dateDue = new Date("Fri Apr 22 2022 18:00:00");
if (dateDue.getMinutes() < 10) {
       var getMinutes= '0' + dateDue.getMinutes();
   }

about 4 years ago · Juan Pablo Isaza Denunciar

0

I could answer half the question if it helps to start thinking process. To put :00 for seconds. You need a loop which checks the condition, if seconds less than 9. If that's the case concatenate 0 to secs.

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