Get Month and Date in 2 Digit Format in JavaScript

avatar

Borislav Hadzhiev

Sat Oct 23 20212 min read

banner

Photo by Chris Yang

Get Month and Date in 2 Digit Format in JavaScript #

To get the month and date in 2 digit format, convert the month and the date to strings and call the padStart() method on them. The padStart method allows us to add leading zeros to a string until it becomes of specific targeted length.

index.js
const date = new Date('March 5, 2025 05:24:00'); const year = date.getFullYear(); console.log(year); // ๐Ÿ‘‰๏ธ 2025 const month = String(date.getMonth() + 1).padStart(2, '0'); console.log(month); // ๐Ÿ‘‰๏ธ 03 const day = String(date.getDate()).padStart(2, '0'); console.log(day); // ๐Ÿ‘‰๏ธ 05 const joined = [day, month, year].join('/'); console.log(joined); // ๐Ÿ‘‰๏ธ 05/03/2025

The first step is to convert the date to a string, so we can call the padStart method.

We used the padStart method to pad the month and the day of a date if necessary.

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.

Because we know that a month and a day of the month can have up to 2 digits, we set the target length to 2.

Note that we have to add 1 to the return value from the getMonth method. This is because the method returns an integer between 0 (January) and 11 (December).

If the month or the day already have 2 digits, the strings will get returned as is, because we've set the target length to 2.

Here's the same example, however this time, both the day and the month have 2 digits, so no leading zeros are added.

index.js
const date = new Date('October 15, 2025 05:24:00'); const year = date.getFullYear(); console.log(year); // ๐Ÿ‘‰๏ธ 2025 const month = String(date.getMonth() + 1).padStart(2, '0'); console.log(month); // ๐Ÿ‘‰๏ธ 10 const day = String(date.getDate()).padStart(2, '0'); console.log(day); // ๐Ÿ‘‰๏ธ 15 const joined = [day, month, year].join('/'); console.log(joined); // ๐Ÿ‘‰๏ธ 15/10/2025
Note that 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 month and date in 2 digit format, check if the month and date are less than 10, if they are, add a leading zero using the addition (+) operator, otherwise return the values directly.

index.js
function getMonth2Digits(date) { // ๐Ÿ‘‡๏ธ Add 1, because getMonth is 0-11 const month = date.getMonth() + 1; if (month < 10) { return '0' + month; } return month; } function getDay2Digits(date) { const day = date.getDate(); if (day < 10) { return '0' + day; } return day; } const date = new Date('April 07, 2025 10:24:06'); console.log(getMonth2Digits(date)); // ๐Ÿ‘‰๏ธ 04 console.log(getDay2Digits(date)); // ๐Ÿ‘‰๏ธ 07

We created 2 reusable functions. In our first function, we have to add 1 to the return value of getMonth, because the method returns integers from 0-11.

Other than that, the logic is the same - we check if the month or date are less than 10 and if they are we prepend a leading 0.

If they aren't we return the values straight away.

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee