Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

107
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda