All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Convert Date To String Format dd/mm/yyyy

Last Updated : Mar 11, 2024

JavaScript Convert Date To String Format dd/mm/yyyy

In this tutorial we will show you the solution of JavaScript convert date to string format dd/mm/yyyy, we will learn about how to convert a date to string format using JavaScript, the format is as dd/mm/yyyy.

Mainly, there are many format with the help of which we are able to print dates, like some are with time format and without time.

We are also able to print date in integer format and also in string. Hence, there are many ways with help of which we are able to print date.

So, today we choose the format dd/mm/yyyy in string. Let us understand this.

Step By Step Guide On JavaScript Convert Date To String Format dd/mm/yyyy :-

As, we also know that there are many method which we can use to give required output. Let us understand that how to covert date format to string and in format dd/mm/yyyy with time also.

In next format we will understand the same without time format. Let us see the codes now.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>convert object to array using JavaScript</title>
</head>
<body>
<h1 style="color: green">
 Talkers code
</h1>
 <script>
function pad2(n) {
  return (n < 10 ? '0' : '') + n;
}
var date = new Date();
var month = pad2(date.getMonth()+1);//months (0-11)
var day = pad2(date.getDate());//day (1-31)
var year= date.getFullYear();
var formattedDate = day+"-"+month+"-"+year;
alert(formattedDate);
</script>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Here, we inside body we write our function which helps us to convert date to string format that is dd/mm/yyyy. Also, it helps us to get current date.
  6. Here, first of all we create a function that return the date in that format.
  7. After that with help of date format we store current date in variable name as date.
  8. Then we divide our current month, date and year as you see with the help of function. Here, month variable holds month value, day variable holds date and year variable holds current year.
  9. Now, we use formatted date and inside this we write the format which we want and alert it. The date got alerted when we loads our webpage.
  10. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to convert date to string format dd/mm/yyyy using JavaScript.

I hope this tutorial on JavaScript convert date to string format dd/mm/yyyy helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪