Open In App

How to get current formatted date dd/mm/yyyy in JavaScript ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

In this article, we will see how to get the format current date in dd/mm/yyyy format by using JavaScript. We’re going to discuss a few methods. The first few methods to know

These are the methods that can be used in solving this problem:

JavaScript getDate() Method

This method returns the day of the month (from 1 to 31) for the defined date. 

Syntax:

Date.getDate();

Return value:

It returns a number, from 1 to 31, representing the day of the month.

JavaScript getFullYear() Method

This method returns the year (four digits for dates between years 1000 and 9999) of the defined date.

Syntax:

Date.getFullYear();

Return value:

It returns a number, representing the year of the defined date

JavaScript getMonth() Method

This method returns the month (from 0 to 11) for the defined date, based on to local time. 

Syntax:

Date.getMonth();

Return value:

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

JavaScript String slice() method

This method gets parts of a string and returns the extracted parts in a new string. It uses the start and end parameters to define the part of the string to extract. First character starts from position 0, the second has position 1, and so on. 

Syntax:

string.slice(start, end);

Parameters:

  • start: This parameter is required. It specifies the position from where to start the extraction. First character is at position 0
  • end: This parameter is optional. It specifies the position (excluding it) where to stop the extraction. If not used, slice() selects all characters from the start-position till the end of string.

Return value:

It returns a string, representing the extracted part of the string.

JavaScript replace() method

This method searches a string for a defined value, or a regular expression, and returns a new string with the replaced defined value. 

Syntax: 

string.replace(searchVal, newvalue);

Parameters: 

  • searchVal: This parameter is required. It specifies the value, or regular expression, that is going to replace by the new value.
  • newvalue: This parameter is required. It specifies the value to replace the search value with.

Return value:

Returns a new string where the defines value(s) has been replaced by the new value.

Example 1: This example format the date in dd/mm/yyyy by checking both date and month, If they are not in 2 digits the zero is added to make them 2 digits. 

Javascript




let today = new Date();
console.log(today);
 
let dd = today.getDate();
let mm = today.getMonth() + 1;
 
let yyyy = today.getFullYear();
 
if (dd < 10) {
    dd = '0' + dd;
}
if (mm < 10) {
    mm = '0' + mm;
}
today = dd + '/' + mm + '/' + yyyy;
 
console.log(today);


Output

2023-12-15T09:43:41.117Z
15/12/2023

Example 2: This example first slice the date part from the date object and then format the date in dd/mm/yyyy. 

Javascript




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


Output

2023-12-15T09:43:41.245Z
15/12/2023

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.



Last Updated : 15 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads