How to format javascript date to yyyy-mm-dd

By Saruque Ahamed Mollick

In this JavaScript tutorial, we gonna learn how to format JavaScript date to YYYY-MM-DD easily. This is a very easy tutorial as you don’t need to work hard to Convert a date to YYYY-MM-DD.

To convert a date to YYYYMMDD in JavaScript, we have a built-in method that we can use.

toISOString();

We will use this method.

Also read,

how to subtract days from a date in javascript

How to compare two dates in JavaScript

Format JavaScript Date to YYYY-MM-DD

We will learn this with an easy example.



<html>
   <head>
      <title>Title Goes Here</title>
   </head>
   <body>
      <script>
         var current_date;
         var new_format_result;
         current_date = new Date();
         new_format_result = current_date.toISOString();
         document.write(new_format_result);
      </script>
   </body>
</html>

Output:

    2018-10-23T17:40:54.680Z

Explanation:

current_date = new Date();

This is used to store system date time in current_date variable

current_date.toISOString();

Now, this will return the Date in ISO standard.

This is known as ISO-8601 having a format like this:

YYYY-MM-DDTHH:mm:ss.sssZ

toISOString() method return a string which represents the date and time in ISO format.

Finally, I would like to say you feel free to comment in the below comment section. Happy coding.

Also read,

How to add minutes to date in the JavaScript date object

how to subtract days from a date in javascript

Leave a Reply

Your email address will not be published. Required fields are marked *