How to format javascript date to yyyy-mm-dd

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

2 responses to “How to format javascript date to yyyy-mm-dd”

  1. Remi says:

    This method return date and time. What if i want only date?

  2. Saruque Ahamed Mollick says:

    To get the only date without time in JavaScript you can simply use toDateString()
    toDateString() will return the date without time in JavaScript

    Here is your solution:
    Use – new_format_result = current_date.toDateString();
    instead of – new_format_result = current_date.toISOString();

Leave a Reply

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