Last Updated : Jul 1, 2023
The Date() method usually used for allow you to get and set the year, month, day, hour, minute, second and millisecond of date objects, using either local time or UTC time.
Using date() method we collecting current date timestamp then we fetched date from that and printed.
Here in script we defined we collecting current date timestamp by appending date object with getTime() method and this will be in milliseconds format.
By using getDate(), getMonth(), getFullYears() all those methods we fetching date, month and year separately and stored on respective variables ‘d,m,y’ in last we displayed result of current timestamp, separated date on console panel.
<!DOCTYPE html> <html> <head> <title>GET DATE FROM TIMESTAMP</title> </head> <body> <script> var curDTS=new Date().getTime(); var d=new Date(curDTS).getDate(); var m=new Date(curDTS).getMonth()+1; var y=new Date(curDTS).getFullYear(); console.log(curDTS); console.log(d+"/"+m+"/"+y); </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 contain 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 we defined variable ‘curDTS’ for storing fetched current date timestamp. Which is done by Date() object and getTime() method.
- We passing timestamp on date object with getDate() method for collect date from timestamp then same as for month, year we passing timestamp on date object with respective methods ‘getMonth(), getFullYear()’.
- Need to confirm one thing is we have to add ‘1’ with getMonth() method then only we can get correct month from timestamp and finally we printed results on console.
- For open console we need to right click on browser then select ‘inspect’ option from list and panel will open there we need to choose console tab.
- 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 program on browser we can’t see the result because we printed result on console.
So we have to open console for that we need to use shortcut ‘ctrl+shift+j’ on browser then at right hand side console panel will open with the results of timestamp and date.
By using this concept we can also get specified any other date timestamp and date.