Change getMinutes() to 2 digit format in JavaScript


Borislav Hadzhiev

Last updated: Oct 23, 2021


Check out my new book

Change getMinutes() to 2 digit format in JavaScript #

To change the getMinutes() method to 2 digit format:

  1. Use the getMinutes() method to get the minutes.
  2. Use the padStart() method to add a leading zero if it's necessary.
  3. The padStart method allows us to add a leading zero to the start of the string.
const date = new Date('October 15, 2025 05:04:00'); const minutes = String(date.getMinutes()).padStart(2, '0'); console.log(minutes); // 👉️ 04

The padStart method has to be used on a string, so the first step is to convert the number of minutes to a string.

We passed the following 2 parameters to the padStart method:

  1. target length - the length of the string the padStart method should return, once it has been padded.
  2. pad string - the string we want to pad our existing string with, in our case - 0.

We know that the minutes should always have a length of 2, so that's what we set as a target length.

If the minutes already have 2 digits, the padStart method would not add an additional leading zero because we've set the target length to 2.
const date = new Date('October 15, 2025 05:24:00'); const minutes = String(date.getMinutes()).padStart(2, '0'); console.log(minutes); // 👉️ 24

The minutes in the date were set to 24, so no leading zero was added.

The padStart method is not supported in Internet Explorer. If you have to support the browser, use the next approach covered in this article.

To get the minutes of a date with a leading zero, check if the minutes are less than or equal to 9, if they are, add a leading zero to the minutes using the addition (+) operator, if they aren't there is no need to add a leading zero.

// Supported in IE const date = new Date('October 15, 2025 05:04:00'); let minutes = date.getMinutes(); minutes = minutes <= 9 ? '0' + minutes : minutes; console.log(minutes); // 👉️ 04

In this example, we used a ternary operator, which is very similar to an if/else statement.

If the minutes are 9 or less, we know that we have to add a leading zero, in all other cases, we leave the minutes as is.

Either approach works just fine, if you have to support Internet Explorer, pick the second one.

Further Reading #

Use the search field on my Home Page to filter through my more than 3,000 articles.