Get Month and Date in 2 Digit Format in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
4 min

banner

# 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 of the given date.
  2. Use the getDate() method to get the day of the month of 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

get month and date in 2 digit format

The code for this article is available on GitHub

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

We used the String.padStart() method to pad the month and the day of the month if necessary.

We passed the following 2 arguments 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.

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

If you have to do this often, define reusable functions.

index.js
function getMonth2Digits(date) { const month = String(date.getMonth() + 1).padStart(2, '0'); return month; } function getDay2Digits(date) { const day = String(date.getDate()).padStart(2, '0'); return day; } const d = new Date('March 5, 2025 05:24:00'); const month = getMonth2Digits(d); console.log(month); // ๐Ÿ‘‰๏ธ 03 const day = getDay2Digits(d); console.log(day); // ๐Ÿ‘‰๏ธ 05
The code for this article is available on GitHub

The getMonth2Digits and getDay2Digits functions take a Date object as a parameter and format the month and day to 2 digits.

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

# Get Month and Date in 2 Digit Format by comparing to 10

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

get month and date in 2 digit format by comparing to 10

The code for this article is available on GitHub

In the getMonth2Digits 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 is less than 10 and if they are, we prepend a leading 0.

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

The Date.getMonth() method returns an integer between 0 (January) and 11 (December) and represents the month for a given date. Yes, unfortunately, the getMonth method is off by 1.

Alternatively, you can use the slice() method.

# Get Month and Date in 2 Digit Format using slice()

This is a four-step process:

  1. Use the getMonth() method to get the month of the given date.
  2. Use the getDate() method to get the day of the month of the given date.
  3. Prepend a leading zero to the output of the methods.
  4. Get the last 2 characters of the result.
index.js
function getMonth2Digits(date) { const month = ('0' + (date.getMonth() + 1)).slice(-2); return month; } function getDay2Digits(date) { const day = ('0' + date.getDate()).slice(-2); return day; } const d = new Date('March 5, 2025 05:24:00'); const month = getMonth2Digits(d); console.log(month); // ๐Ÿ‘‰๏ธ 03 const day = getDay2Digits(d); console.log(day); // ๐Ÿ‘‰๏ธ 05

get month and date in 2 digit format using slice

The code for this article is available on GitHub

We used the String.slice() method to format the month and date to 2 digits.

Notice that we always add a leading zero to the output of the date.getMonth() and date.getDate() methods, even if the month and day 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' + '12').slice(-2)); // ๐Ÿ‘‰๏ธ 12 console.log(('0' + '2').slice(-2)); // ๐Ÿ‘‰๏ธ 02

The approach works regardless if the month and day consist of 1 or 2 digits.

# 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