Last updated: Mar 3, 2024
Reading timeยท4 min
To get the month and date in a 2-digit format:
getMonth()
method to get the month of the given date.getDate()
method to get the day of the month of the given date.padStart()
method to get the values in a 2-digit format.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 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:
padStart
method will return a string of this length
once it has been padded.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).
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.
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.
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 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.
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.
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
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.
slice()
This is a four-step process:
getMonth()
method to get the month of the given date.getDate()
method to get the day of the month of the given date.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
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:
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' + '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.
You can learn more about the related topics by checking out the following tutorials: