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

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

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 Report

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 Report

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 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!