Get Month and Date in 2 Digit Format in JavaScript

avatar

Borislav Hadzhiev

Last updated: Aug 30, 2022

banner

Photo from Unsplash

Get Month and Date in 2 Digit Format in JavaScript #

To get the month and date in a 2-digit format:

  1. Use the getMonth() method to get the month for the given date.
  2. Use the getDate() method to get the day of the month for the given date.
  3. Use the padStart() method to get the values in a 2-digit format.
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 the month 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.

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

Note that we have to add 1 to the return value of 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 get returned as is, because we've set the target length parameter to 2.

Here's the same example where 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

As an alternative to the padStart() method, you could use a more manual approach.

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 the first function, we have to add 1 to the return value of getMonth because the method returns an integer from 0 to 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 #

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.