Last Updated : Jan 1, 2023
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.
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'); var t=new Date().toTimeString().split(' '); console.log(td+' '+t); </script> </body> </html>
- <!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.
- The<html> tag is used to indicate the beginning of HTML document.
- 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.
- 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.
- <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
- 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’.
- 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’.
- Finally we printed variable ‘td’ with ‘t’ and both are concatenated by ‘+’ with some space so we gets result format.
- Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.
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.