How to Get Today’s Date in dd/mm/yyyy Format in JavaScript?

Do you want to know how to get the current or today’s date in the MM/DD/YYYY format in JavaScript? Keep on reading!

In this article, we will present various JavaScript methods to obtain the current date in the format of DD/MM/YYYY.

So, bear with us as we delve into several techniques to achieve this objective.

What are the JavaScript Date Objects?

Before we move forward, let’s get to know the Date object in JavaScript.

This object offers a range of methods and properties that enable us to handle dates and times effectively, making it a valuable resource for working with dates.

getDate() Method

This method gives the day of the month, whether it’s the 1st, the 15th, or any other day between 1 and 31.

Syntax:

 Date.getDate()

It returns a number from 1 to 31, indicating the day.

getFullYear() Method

This method gives the year of a particular date, but it’s always shown as a four-digit number.

This works for dates from the year 1000 to 9999.

Syntax:

 Date.getFullYear()✅

It returns a number that represents the year.

getMonth() Method

This method gives the month of a date, from 0 to 11, with 0 being January and 11 being December. It considers your local time zone.

Syntax:

Date.getMonth() ✅

It returns a number from 0 to 11, indicating the month.

How to Get Today’s Date in dd/mm/yyyy Format in JavaScript?

Here are a few different ways to get the current date or today’s date in the DD/MM/YYYY format using JavaScript, to get today’s date.

Solution 1: Use the getDate(), getMonth(), and getFullYear() methods

Here’s an example:

let today = new Date();
let dd = String(today.getDate()).padStart(2, '0'); 
let mm = String(today.getMonth() + 1).padStart(2, '0'); 
let yyyy = today.getFullYear();

today = dd + '/' + mm + '/' + yyyy;
console.log(today);

As you can see, in our first solution, we first create a new Date object. Then, we get the day, month, and year from this object.

And the padStart() function is used to add a leading zero to single-digit days and months.

Finally, we concatenate these values into a string in the DD/MM/YYYY format.

Output:

20/09/2023

If you are wondering, why there’s 2 in the padStart?

Here’s the explanation:

The padStart() method in JavaScript is used to pad a string with another string until the resulting string reaches a certain length.

The first argument to padStart() is the total length you want the resulting string to be, and the second argument is the string you want to pad with.

In this case, padStart(2, ‘0’) is used to ensure that the day and month always have two digits.

If the day or month is a single digit (like 1, 2, 3, etc.), padStart() will add a ‘0’ in front of it, making it ‘01’, ‘02’, ‘03’, etc. If the day or month already has two digits (like 10, 11, 12, etc.), padStart() won’t add anything because the string is already 2 characters long.

So, in simple words, the 2 in .padStart(2, ‘0’) specifies that we want our resulting string to be 2 characters long.
The ‘0’ in .padStart(2, ‘0’) is what we pad our original string with to reach that length.

Is it clear now? Let’s proceed to the next solution.

Solution 2: Use the toLocaleDateString() method

Here’s an example:

let today = new Date();
today = today.toLocaleDateString('en-GB');  ✅
console.log(today);

The toLocaleDateString() method converts a date to a string, using locale settings. The ‘en-GB’ argument specifies that we want to use British English date formatting, which is DD/MM/YYYY.

Output:

let today = new Date();
today = today.toLocaleDateString('en-GB'); 
console.log(today);

Solution 3: Using the toJSON() and slice() methods

Here’s an example code:

let today = new Date();
let date = today.toJSON().slice(0,10); 
let formattedDate = date.slice(8,10) + '/' + date.slice(5,7) + '/' + date.slice(0,4);
console.log(formattedDate);

The toJSON() method returns a string representing the Date object in JavaScript Object Notation (JSON) format, which is YYYY-MM-DD.

The slice() method is then used to rearrange this string into the DD/MM/YYYY format.

Output:

20/09/2023

Conclusion

In conclusion, we have provided a variety of methods to get today’s date in mm/dd/yyyy format using JavaScript, making it a versatile language for date manipulation.

The three methods discussed in this article – using getDate(), getMonth(), and getFullYear() methods, the toLocaleDateString() method, and the toJSON() and slice() methods, offer different ways to get today’s date in the DD/MM/YYYY format.

Each method has its own strengths and can be chosen based on your specific requirements.

We hope this article has provided you with enough information to understand how to get today’s date in mm/dd/yyyy format in JavaScript.

If you want to explore more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.

Leave a Comment