Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!