How to useIntl.DateTimeFormat in Javascript
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
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