JavaScript

Get Month and Date in 2 Digit Format in JavaScript

In some situations, the date and month needed to get in a specified format, such as the 2-digit is the most common format. In JavaScript, the Date object offers different methods to get the date, month, and year, such as “getMonth()”, “getDate()”, and “getYear()”. To return the date and month in 2 digits, JavaScript provides the “padStart()” method or the “slice()” method.

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:

padStart(length, padStr)

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:

var date = new Date('Jan 8, 2022');

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”:

var monthIn2Digit = String(date.getMonth() + 1).padStart(2, '0');

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”:

var dateIn2Digit = String(date.getDate()).padStart(2, '0');

Finally, print the month and the date on the console using the “console.log()” method:

console.log("Month in 2 digits " + monthIn2Digit);
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:

slice(start, end)

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:

var monthIn2Digit = ("0" + (date.getMonth() + 1)).slice(-2);

For date, call the getDate() method with slice() method and store the resultant date in “dateIn2Digit”:

var dateIn2Digit = ("0" + date.getDate()).slice(-2);

Print the date and month on the console:

console.log("Month in 2 digits " + monthIn2Digit);
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.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.