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

106
Vistas
Issue with getting the exact current time using jquery

I'm working on an employee attendance system.

I have an issue with time format. When the current time is for example: 12:03, 11:06 ... the code I use displays only 12:3, 11:6 and I couldn't figure out why it doesn't show the zero.

Here is the code:

HTML code:

<a href="#" data-id="<?= $employee['id'] ?>" class="btn btn-soft-danger btn-sm" id="checkOut"><i class="fa fa-plus-circle  fa-lg"></a>

JQUERY:

$(document.body).on('click', '#checkOut', function(e){
        e.preventDefault()
        var dt = new Date();
        var time = dt.getHours() + ":" + dt.getMinutes();
        $(this).prop("disabled", true);
        $(this).removeClass("btn-soft-danger");
        $(this).addClass("btn-light");
        $(this).html(time)
        const Toast = Swal.mixin({
            toast: true,
            position: 'top-end',
            showConfirmButton: false,
            timer: 5000,
            timerProgressBar: true,
            didOpen: (toast) => {
            toast.addEventListener('mouseenter', Swal.stopTimer)
            toast.addEventListener('mouseleave', Swal.resumeTimer)
            }
        })                
        Toast.fire({
            icon: 'success',
            title: 'The attendance saved successfully.'
        });
    });
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

When you read time data like minutes/seconds etc... you getting numbers, so 11:06 -> 6 minutes, numbers don't hold "0" from it starts.

You need to check, if number is less then 10 - add "0" at start:

var time = String(dt.getHours()).padStart(2, "0") + ":" + String(dt.getMinutes()).padStart(2, "0");

Here i convert hours/minutes to string and if it length is less then 2 - add "0" from it start(by using .padStart() method).

Make from this check separate function or how you would like, but do check if number is less then 10.

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