All TalkersCode Topics

Follow TalkersCode On Social Media

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

Get Date From Timestamp JavaScript

Last Updated : Mar 11, 2024

Get Date From Timestamp JavaScript

In this tutorial we will show you the solution of get date from timestamp JavaScript, here we needs to use Date() method in JavaScript.

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.

Step By Step Guide On Get Date From Timestamp JavaScript :-

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>
  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 contain 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 we defined variable ‘curDTS’ for storing fetched current date timestamp. Which is done by Date() object and getTime() method.
  7. 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()’.
  8. 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.
  9. 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.
  10. 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 date from timestamp using javascript.

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.

I hope this tutorial on get date from timestamp JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪