Categories
JavaScript Answers

How to display two digit numbers with JavaScript date getMinutes?

Spread the love

Sometimes, we want to display two digit numbers with JavaScript date getMinutes.

In this article, we’ll look at how to display two digit numbers with JavaScript date getMinutes.

How to display two digit numbers with JavaScript date getMinutes?

To display two digit numbers with JavaScript date getMinutes, we can use the string padStart method.

For instance, we write

const minutes = String(date.getMinutes()).padStart(2, "0");

to get the minutes from the date with getMinutes.

We convert the number returned by getMinutes to a string with String.

Then we call padStart with 2 and '0' to prepend '0'‘s to the string to make the string length 2 if needed.

Conclusion

To display two digit numbers with JavaScript date getMinutes, we can use the string padStart method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *