Last updated: Mar 3, 2024
Reading timeยท6 min
To change the getMinutes()
method to a 2-digit format:
getMinutes()
method to get the minutes.padStart()
method to add a leading zero if it's necessary.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 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:
padStart
method will return a string of this length
once it has been padded.0
.We know that the minutes should always have a length of 2
, so that's what we
set as a target length.
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
If you have to do this often, define a reusable function.
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 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.
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.
const date = new Date('October 15, 2025 05:04:00'); let minutes = date.getMinutes(); minutes = minutes <= 9 ? '0' + minutes : minutes; console.log(minutes); // ๐๏ธ 04
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.
slice()
This is a three-step process:
getMinutes()
method to get the minutes.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
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:
Name | Description |
---|---|
start index | The index of the first character to include in the returned substring |
end index | The index of the first character to exclude from the returned substring |
The String.slice()
method can be passed negative indexes to count backward.
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.
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.
To change the getHours()
method to 2 digit format:
getHours()
to a string.padStart()
method to add a leading zero if it's necessary.padStart
method allows us to add a leading zero to the start of the
string.const date = new Date('September 24, 2025 05:24:00'); const hours = String(date.getHours()).padStart(2, '0'); console.log(hours); // ๐๏ธ 05
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:
padStart
method should
return, once it has been padded.0
.We know that the hours should always have a length of 2
, so that's what we set
as a target length.
padStart
method would not add an additional leading zero because we've set the target length
to 2
.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
.
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.
To change the getSeconds()
method to 2 digit format:
getSeconds()
to a string.padStart()
method to add a leading zero if it's necessary.padStart
method allows us to add a leading zero to the start of the
string.const date = new Date('March 14, 2025 05:24:07'); const seconds = String(date.getSeconds()).padStart(2, '0'); console.log(seconds); // ๐๏ธ 07
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:
padStart
method should
return, once it has been padded.0
.We know that the seconds should always have a length of 2
, so that's what we
set as a target length.
padStart
method would not add an additional leading zero because we've set the target length
to 2
.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
.
const date = new Date('September 24, 2025 05:24:06'); let seconds = date.getSeconds(); seconds = seconds <= 9 ? '0' + seconds : seconds; console.log(seconds); // ๐๏ธ 06
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.
You can learn more about the related topics by checking out the following tutorials: