How to get Month and Date of JavaScript in two digit format ?
To get the Month and Date of JavaScript in two-digit format, we have multiple approaches. In this article, we are going to learn how to get the Month and Date of JavaScript in a two-digit format.
Below are the approaches used to get the Month and Date of JavaScript in two-digit format:
Table of Content
- Approach 1: Using padStart() method
- Approach 2: Using toLocaleString with options
- Approach 3: Using slice() Method
- Approach 4: Using Intl.DateTimeFormat
Approach 1: Using padStart() method
The padStart() method in JavaScript is used to pad a string with another string until it reaches the given length. The padding is applied from the left end of the string.
Syntax:
string.padStart(targetLength, padString);
Example: In this example, padStart(2, '0')
: This ensures that the month and date have at least two characters by adding leading zeros if needed.
const currentDate = new Date();
const month = (currentDate.getMonth() + 1).toString().padStart(2, '0');
const day = currentDate.getDate().toString().padStart(2, '0');
console.log(`Current Date: ${month}/${day}`);
Output
Current Date: 01/10
Approach 2: Using toLocaleString with options
The date.toLocaleString()method is used to convert a date and time to a string using the locale settings.
Syntax:
dateObj.toLocaleString(locales, options);
Example: In this example, w
e are using toLocaleString
method.
const currentDate = new Date();
const month =
(currentDate.getMonth() + 1).toLocaleString('en-US',
{ minimumIntegerDigits: 2, useGrouping: false });
const day =
currentDate.getDate().toLocaleString('en-US',
{ minimumIntegerDigits: 2, useGrouping: false });
console.log(`Current Date: ${month}/${day}`);
Output
Current Date: 01/10
Approach 3: Using slice() Method
The slice()
method in JavaScript is used to extract a portion of a string and create a new string without modifying the original string.
Syntax:
string.slice(startingIndex, endingIndex);
Example: In this example, ('0' + (currentDate.getMonth() + 1)).slice(-2)
: Prepends a ‘0’ to the month value, converts it to a string, and then extracts the last two characters using slice()
. ('0' + currentDate.getDate()).slice(-2)
: Prepends a ‘0’ to the date value, converts it to a string, and extracts the last two characters using slice()
.
const currentDate = new Date();
const month = ('0' + (currentDate.getMonth() + 1)).slice(-2);
const day = ('0' + currentDate.getDate()).slice(-2);
console.log(`Current Date: ${month}/${day}`);
Output
Current Date: 01/10
Approach 4: Using Intl.DateTimeFormat
The Intl.DateTimeFormat object allows for formatting dates according to locale-specific conventions. This method is versatile and provides a simple way to format dates with leading zeros.
Syntax:
new Intl.DateTimeFormat('en-US', options).format(date);
Example: This example demonstrates how to use Intl.DateTimeFormat to get the month and date in a two-digit format.
const currentDate = new Date();
const month = new Intl.DateTimeFormat('en-US', { month: '2-digit' }).format(currentDate);
const day = new Intl.DateTimeFormat('en-US', { day: '2-digit' }).format(currentDate);
console.log(`Current Date: ${month}/${day}`);
Output
Current Date: 05/31
Contact Us