There are many ways to get month and date in 2 digit format in JavaScript. In this article, I will introduce two ways to use conditional statements. Another way is to use some built-in functions.
How to get Month and Date in 2 digit format in JavaScript
Use conditional statements
We can use a conditional statement to quickly check the Month and Date in 2 digit format.
Example:
function getDateMonth2Digit(dateCheck) { let day = dateCheck.getDate(); if (day < 10) { day = '0' + day; } let month = dateCheck.getMonth() + 1; if (month < 10) { month = '0' + month; } return (day + '/' + month + '/' + dateCheck.getFullYear()); } var date1 = new Date('2020/3/1'); var date2 = new Date('2020/11/23'); console.log(getDateMonth2Digit(date1)); console.log(getDateMonth2Digit(date2));
Output:
01/03/2020
23/11/2020
In the above example, I create a function that checks the passed date and outputs Month and Date in 2 digit format with the input parameter of a date we need to check and change the date format to DD/MM. The function will first get the Day in the passed dateCheck
variable by the getDate()
method in JS, then compare it with 10. If less, we will add a leading 0 by adding the character '0'
. We will get the number of days formatted with two characters. For the Month, it is no different. Still, each time you use to date, you must add one because, in the getMonth()
method, the winning number is represented (0->11). Finally, we output a string that is the date we need to check with the format DD/MM/YYYY.
Use the padStart() method
The padStart()
method is a built-in function in Javascript, which pads a string in front of a number and converts it to a string. The return type will be a string with the number of characters as the parameter we pass in the Initial number and parameter padding string.
Syntax:
padStart(totalLength, padString)
Parameters:
- totalLength: This is the total length of the string you want to add padding string.
- padString: This is the string you want to add when the number of characters is not equal to totalLength.
Example:
function getDateMonth2Digit(dateCheck) { let day = String(dateCheck.getDate()).padStart(2, "0"); let month = String(dateCheck.getMonth() + 1).padStart(2, "0"); return (day + '/' + month + '/' + dateCheck.getFullYear()); } var date1 = new Date('2020/3/2'); var date2 = new Date('2020/10/22'); console.log(getDateMonth2Digit(date1)); console.log(getDateMonth2Digit(date2));
Output:
02/03/2020
22/10/2022
After running the above function, we see that the return result is a date in the format DD/MM/YYYY with days less than 10. By default, we will add zeros to the beginning of the day with the padStart()
method. Similarly, the program adds zeros to the beginning of the month that are less than 10. After processing, we combine the string and output the date formated as DD/MM/YYYY.
Summary
I have shown you several ways to get Month and Date in 2 digit format in JavaScript in the above tutorial. To understand and apply, you should practice; this will help you understand and remember it longer. I hope this article is helpful for you. Good luck for you!
Maybe you are interested:
- Validate a Date formatted as DD/MM/YYYY in JavaScript
- Get the Start and End of the Day using JavaScript
- Get Year, Month and Day from a Date Object in JavaScript

My name is Tom Joseph, and I work as a software engineer. I enjoy programming and passing on my experience. C, C++, JAVA, and Python are my strong programming languages that I can share with everyone. In addition, I have also developed projects using Javascript, html, css.
Job: Developer
Name of the university: UTC
Programming Languages: C, C++, Javascript, JAVA, python, html, css