All TalkersCode Topics

Follow TalkersCode On Social Media

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

JavaScript Date Format yyyy-mm-dd hh mm ss

Last Updated : Mar 11, 2024

JavaScript Date Format yyyy-mm-dd hh mm ss

In this tutorial we will show you the solution of JavaScript date format yyyy-mm-dd hh mm ss, here we used to toISOString(), toTimeString() methods for get the date format.

The toISOString() method returns a string in simplified extended ISO format which is always 24 or 27 characters long (YYYY-MM-DDTHH:mm:ss. sssZ).

The toTimeString() method is used to return the time portion of the given object in English.

Step By Step Guide On Javascript Date Format yyyy-mm-dd hh mm ss :-

Here we defined date object using date() constructor then we appends ‘toISOString()’ method for retrieve current date.

The split method used to split some characters or word from string here we used to splits strings from the beginning to letter ‘T’ and stored to variable ‘td’.

Same as above method we appended ‘toTimeString()’ method with date object we know it return current time with long string so we used split() method for split hour,minutes,seconds from the long string then result stored to variable ‘t’.

Finally result displayed on console.

<!DOCTYPE html>
<html>
    <head>
        <title>Date YYYY-MM-DD HH MM SS</title>
    </head>
    <body>
        <script>
            var td=new Date().toISOString().split('T')[0];
            var t=new Date().toTimeString().split(' ')[0];
            console.log(td+' '+t);
        </script>
    </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contains information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. In script tag we defined date object with ‘toISOString()’ method for collect current date with split() method. Here split() method used to collect date format only from rest of string and stored to variable ‘td’.
  7. We aappends date object with ‘toTimeString()’ method for collect current time with split() method. Here split() method used to collect time format only from rest of string and stored to variable ‘t’.
  8. Finally we printed variable ‘td’ with ‘t’ and both are concatenated by ‘+’ with some space so we gets result format.
  9. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to get expected format date and time in javascript.

When we executes our program on browser we can’t see result because we displayed result on console using ‘console.log()’ method.

For open console we need to use shortcut ‘ctrl+shift+j’ then right hand side console panel will open with our result.

We can also append this result on webpage using element id attribute and innerHTML.

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

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪