This post will describe the methods for getting the date and month in a 2-digit format using JavaScript.
How to Get Month and Date in 2 Digit Format in JavaScript?
To get the date and month in 2-digit format, use the below-provided JavaScript predefined methods:
- padStart() method
- slice() method
Let’s see how these methods will work.
Method 1: Get Month and Date in 2 Digit Format Using padStart() Method
For getting the date and month in 2-digit format, use the “padStart()” method with the “getMonth()” and “getDate()” methods. The getDate() method gives the day of the month (from 1 to 31) for the particular date, whereas the getMonth() method gives the month (based on local time) for the specified date. The padStart() method appends another string to the existing string until it reaches the defined length.
Syntax
The following syntax is used for the padstart() method:
In the above syntax:
- The “length” is the defined length of the resultant string.
- “padStr” is the string that will append.
- Here, we don’t need to append any string with a date so, we will pass “0” as a padString.
Example
First, create a date object by passing the date in a constructor of the Date Object:
Call the “getMonth()” method to get the month of the specified date and then call the “padStart()” method by passing “2” as the length of the date which is the first argument and “0” as a second argument which sets month in 2-digit format and stores it in variable “monthIn2Digit”:
Here, we have added 1 to the return value of the getMonth method because the getMonth() method outputs an integer between 0 (January) and 11 (December).
Call the “getDate()” method with the “padStart()” method by passing “2” as the length of the date, which is the first argument, and “0” as a second argument and store it in the variable “dateIn2Digit”:
Finally, print the month and the date on the console using the “console.log()” method:
console.log("Date in 2 digits " + dateIn2Digit);
The output indicates that the date and month have been successfully fetched in 2 digit format:
Method 2: Get Month and Date in 2 Digit Format Using slice() Method
To get the date and month in 2-digit format, use the “slice()” method with the “getMonth()” and “getDate()” methods. The slice() method cuts a part of a string and outputs it as a new string.
Syntax
Follow the given syntax for the slice() method:
Here:
- “start” indicates the starting point for the extraction. It is an essential parameter.
- “end” defines the point at which the extraction should end. It is an optional parameter.
Example
Call the getMonth() method with the slice() method by passing “-2” as an argument to get the month in 2-digit format. As the slice() method only extracts the strings, “0” is used to append before the resultant month and slice it to length 2. It will help to get the month from 0-9 in two digits:
For date, call the getDate() method with slice() method and store the resultant date in “dateIn2Digit”:
Print the date and month on the console:
console.log("Date in 2 digits " + dateIn2Digit);
Output
We have compiled the necessary information related to getting the month and date in a 2-digit format in JavaScript.
Conclusion
To get the date and month in 2-digit format, use the “padStart()” method or the “slice()” method with the “getMonth()” and “getDate()” methods. The getMonth() and the getDate() methods give the date and month of the specified date, and the padStart() or the slice() methods give the resultant date and month in 2 digits format. This post described the methods for getting the date and month in a 2-digit format using JavaScript.