How to get Month and Date of JavaScript in two digit format ?
Last Updated :
31 May, 2024
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:
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.
JavaScript
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}`);
OutputCurrent 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.
JavaScript
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}`);
OutputCurrent Date: 01/10
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()
.
JavaScript
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}`);
OutputCurrent Date: 01/10
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.
JavaScript
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}`);
OutputCurrent Date: 05/31
Similar Reads
How to get seconds since epoch in JavaScript? Given a date, we have to find the number of seconds since the epoch (i.e. 1 January 1970, 00:00:00 UTC ). The getTime() method in the JavaScript returns the number of milliseconds since January 1, 1970, or epoch. If we divide these milliseconds by 1000 and then integer part will give us the number o
1 min read
How to check if one date is between two dates in JavaScript ? The task is to determine if the given date is in between the given 2 dates or not? Here are a few of the most used techniques discussed with the help of JavaScript. In the first approach, we will use .split() method and the new Date() constructor. And in the second approach we will use the .getTime(
3 min read
How to check if the given date is weekend ? To check if a given date falls on a weekend in JavaScript, you can use the getDay() method on a Date object. This method returns a number representing the day of the week, where 0 is Sunday and 6 is Saturday.There are two methods to solve this problem which are discussed below: Table of ContentUsing
2 min read
How to Convert Date to Another Timezone in JavaScript? Converting a date to another timezone in JavaScript means adjusting the local date and time to match the time in a different timezone. This ensures that the displayed time aligns with the selected timezone, often using built-in methods or external libraries.1. Using Intl.DateTimeFormat() and format(
2 min read
How to check if date is less than 1 hour ago using JavaScript ? Given a date and the task is to check whether the given date is less than 1 hour ago or not with the help of JavaScript. Approach 1: Count the milliseconds of the difference between the current and prev_date.If those are greater than milliseconds in 1 hour, then it returns false otherwise returns tr
2 min read
How to check a date is valid or not using JavaScript? To check if a date is valid or not in JavaScript, we have to know all the valid formats of the date. For ex - "YYYY/DD/MM", "DD/MM/YYYY", and "YYYY-MM-DD", etc. We have a given date format and we need to check whether the given format is valid or not according to the official and acceptable date for
3 min read
How to get the day and month of a year using JavaScript ? Given a date and the task is to get the day and month of a year using JavaScript. Approach: First get the current date by using new Date().Use getDay() method to get the current day in number format, Map it to the day name.Use getMonth() to get the current month in number format, Map it to the month
2 min read
How to calculate the date three months prior using JavaScript ? To calculate the date three months prior using JavaScript, we could use the getMonth() and setMonth() methods. In this article, we are going to learn how to calculate the date three months prior using JavaScript. ApproachFirst, select the date object.Then use the getMonth() method to get the months.
1 min read
How to calculate the yesterday's date in JavaScript ? In this article, we will see how to calculate yesterdayâs date in JavaScript. To calculate yesterday's date, you need to have some basic ideas of a few methods of JavaScript. JavaScript getDate() MethodJavaScript setDate() MethodJavaScript getDate() Method: It is an inbuilt JavaScript function that
3 min read
How to calculate minutes between two dates in JavaScript ? Given two dates and the task is to get the number of minutes between them using JavaScript. Approach: Initialize both Date object.Subtract the older date from the new date. It will give the number of milliseconds from 1 January 1970.Convert milliseconds to minutes. Example 1: This example uses the c
2 min read