Open In App

How to Get Current Formatted Date dd/mm/yyyy in JavaScript?

Last Updated : 04 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Here are different ways to get the current date in dd/mm/yyyy format.

1. Using Native JavaScript Methods

JavaScript's built-in Date object provides various methods to work with dates and times. You can extract individual components like day, month, and year to format the date manually.

JavaScript
// Get current date
const today = new Date();

// Extract day, month, and year
let day = today.getDate();
let month = today.getMonth() + 1;
let year = today.getFullYear();

// Add leading zero to day and month if needed
day = day < 10 ? '0' + day : day;
month = month < 10 ? '0' + month : month;

// Format the date as dd/mm/yyyy
const formattedDate = `${day}/${month}/${year}`;

console.log(formattedDate);

Output
04/12/2024

2. Using Intl.DateTimeFormat() Constructor

The Intl.DateTimeFormat() constructor is a built-in JavaScript object that enables language-sensitive date and time formatting. By specifying the desired locale and format options, we can obtain the formatted date in the desired format.

JavaScript
const today = new Date();

const formattedDate = new Intl.DateTimeFormat('en-GB', {
  	day: '2-digit',
  	month: '2-digit',
  	year: 'numeric'
}).format(today);

console.log(formattedDate);

Output
04/12/2024

3. Using toLocaleDateString() Method

The toLocaleDateString() method formats the date according to the specified locale.

JavaScript
const today = new Date();
const formattedDate = today.toLocaleDateString('en-GB');
console.log(formattedDate);

Output
04/12/2024

4. Using a Custom Function

If you prefer a reusable function for formatting dates, you can create your own utility.

JavaScript
function getFormattedDate(date) {
  let day = date.getDate();
  let month = date.getMonth() + 1;
  let year = date.getFullYear();

  // Add leading zeros
  day = day < 10 ? '0' + day : day;
  month = month < 10 ? '0' + month : month;

  return `${day}/${month}/${year}`;
}

const today = new Date();
console.log(getFormattedDate(today));

Output
04/12/2024

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.


Next Article

Similar Reads