Change getMinutes, getHours & getSeconds to 2-digit format

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
6 min

banner

# Table of Contents

  1. Change getMinutes() to two-digit format in JavaScript
  2. Change getHours() to 2-digit format in JavaScript
  3. Change getSeconds() to 2-digit format in JavaScript

# Change getMinutes() to two-digit format in JavaScript

To change the getMinutes() method to a 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.
index.js
const date = new Date('October 15, 2025 05:04:00'); const minutes = String(date.getMinutes()).padStart(2, '0'); console.log(minutes); // ๐Ÿ‘‰๏ธ 04

change getminutes to two digit format

The code for this article is available on GitHub

The String.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 padStart method will return a string of this length once it has been padded.
  2. pad string - the string we want to pad our existing string with, in our case a 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.
index.js
const date = new Date('October 15, 2025 05:24:00'); const minutes = String(date.getMinutes()).padStart(2, '0'); console.log(minutes); // ๐Ÿ‘‰๏ธ 24

If you have to do this often, define a reusable function.

index.js
function getMinutes2Digits(date) { return String(date.getMinutes()).padStart(2, '0'); } const date = new Date('October 15, 2025 05:04:00'); console.log(getMinutes2Digits(date)); // ๐Ÿ‘‰๏ธ 04
The code for this article is available on GitHub

The getMinutes2Digits function takes a Date object as a parameter and formats the minutes to 2 digits.

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

# Change getMinutes() to a two-digit format by comparing to 10

Alternatively, you can check if the minutes are less than or equal to 9.

If they are, add a leading zero to the minutes, if they aren't, there is no need to add a leading zero.

index.js
const date = new Date('October 15, 2025 05:04:00'); let minutes = date.getMinutes(); minutes = minutes <= 9 ? '0' + minutes : minutes; console.log(minutes); // ๐Ÿ‘‰๏ธ 04
The code for this article is available on GitHub

We used the ternary operator to check if the minutes are fewer than 10.

If the expression to the left of the question mark is truthy, the operator returns the value to the left of the colon, otherwise, the value to the right of the colon is returned.

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

# Change getMinutes() to a two-digit format by using slice()

This is a three-step process:

  1. Use the getMinutes() method to get the minutes.
  2. Prepend a leading zero to the output of the method.
  3. Get the last 2 characters of the result.
index.js
function getMinutes2Digits(date) { const minutes = ('0' + date.getMinutes()).slice(-2); return minutes; } const date = new Date('October 15, 2025 05:04:00'); console.log(getMinutes2Digits(date)); // ๐Ÿ‘‰๏ธ 04
The code for this article is available on GitHub

We used the String.slice() method to format the minutes to 2 digits.

Notice that we always add a leading zero to the output of the date.getMinutes() method, even if the minutes already have 2 digits.

This is why we used the String.slice() method to get the last 2 characters of the string.

The String.slice() method extracts a section of a string and returns it, without modifying the original string.

The String.slice() method takes the following arguments:

NameDescription
start indexThe index of the first character to include in the returned substring
end indexThe index of the first character to exclude from the returned substring

The String.slice() method can be passed negative indexes to count backward.

index.js
const str = 'bobbyhadz.com'; console.log(str.slice(-3)); // ๐Ÿ‘‰๏ธ com console.log(str.slice(-2)); // ๐Ÿ‘‰๏ธ om

We always add a leading zero to the month or date and take the last 2 characters.

index.js
console.log(('0' + '24').slice(-2)); // ๐Ÿ‘‰๏ธ 24 console.log(('0' + '4').slice(-2)); // ๐Ÿ‘‰๏ธ 04

The approach works regardless if the minutes consist of 1 or 2 digits.

# Table of Contents

  1. Change getHours() to 2-digit format in JavaScript
  2. Change getSeconds() to 2-digit format in JavaScript

# Change getHours() to 2-digit format in JavaScript

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

  1. Convert the result of calling getHours() to a string.
  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.
index.js
const date = new Date('September 24, 2025 05:24:00'); const hours = String(date.getHours()).padStart(2, '0'); console.log(hours); // ๐Ÿ‘‰๏ธ 05

change gethours to 2 digit format

The code for this article is available on GitHub
If you need to change the getSeconds() method to a two-digit format, scroll down to the next subheading.

The String.padStart() method has to be used on a string, so the first step is to convert the number of hours 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 hours should always have a length of 2, so that's what we set as a target length.

If the hours already have 2 digits, the padStart method would not add an additional leading zero because we've set the target length to 2.
index.js
const date = new Date('September 24, 2025 15:24:00'); const hours = String(date.getHours()).padStart(2, '0'); console.log(hours); // ๐Ÿ‘‰๏ธ 15

The hours were already set to 15 (2 digits), so the padStart() method didn't add a leading zero.

Alternatively, you can check if the hours are fewer than 10.

index.js
const date = new Date('September 24, 2025 05:24:00'); let hours = date.getHours(); hours = hours <= 9 ? '0' + hours : hours; console.log(hours); // ๐Ÿ‘‰๏ธ 05

We used a ternary operator which is very similar to an if/else statement.

If the hours are 9 or less, we add a leading zero, otherwise, we leave the hours as is.

# Change getSeconds() to 2-digit format in JavaScript

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

  1. Convert the result of calling getSeconds() to a string.
  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.
index.js
const date = new Date('March 14, 2025 05:24:07'); const seconds = String(date.getSeconds()).padStart(2, '0'); console.log(seconds); // ๐Ÿ‘‰๏ธ 07

change getseconds to 2 digit format

The code for this article is available on GitHub

The String.padStart() method has to be used on a string, so the first step is to convert the number of seconds 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 a 0.

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

If the seconds already have 2 digits, the padStart method would not add an additional leading zero because we've set the target length to 2.
index.js
const date = new Date('March 14, 2025 13:24:22'); const seconds = String(date.getSeconds()).padStart(2, '0'); console.log(seconds); // ๐Ÿ‘‰๏ธ 22

The seconds were set to 22 (2 digits), so the padStart method didn't add a leading zero.

Alternatively, you can check if the seconds are fewer than 10.

index.js
const date = new Date('September 24, 2025 05:24:06'); let seconds = date.getSeconds(); seconds = seconds <= 9 ? '0' + seconds : seconds; console.log(seconds); // ๐Ÿ‘‰๏ธ 06
The code for this article is available on GitHub

We used a ternary operator, which is very similar to an if/else statement.

If the seconds are 9 or less, we add a leading zero, otherwise, we return the seconds as is.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev