JavaScript | Converting milliseconds to date. - GeeksforGeeks

26 Jul, 2021
Given a number of milliseconds, The task is to convert them to date using javascript. we’re going to discuss few techniques.
Approach :
First declare variable time and store the milliseconds of current date using new date() for current date and getTime() Method for return it in milliseconds since 1 January 1970.
Convert time into date object and store it into new variable date.
Convert the date object’s contents into a string using date.toString() function
Example 1: This example first gets the milliseconds of the current date and time, Then using that value to get the date by Date() method.
<!DOCTYPE HTML><html>  <head>    <title>        JavaScript       | Converting milliseconds to date.    </title></head>  <body style="text-align:center;" id="body">    <h1 style="color:green;">              GeeksForGeeks          </h1>    <p id="GFG_UP" style="font-size: 19px;               font-weight: bold;">    </p>    <button onClick="GFG_Fun()">        click here    </button>    <p id="GFG_DOWN" style="color: green;              font-size: 24px;              font-weight: bold;">    </p>    <script>        var up = document.getElementById('GFG_UP');        var down = document.getElementById('GFG_DOWN');        var time = new Date().getTime();        up.innerHTML = "Milliseconds = " + time;          function GFG_Fun() {            var date = new Date(time);            down.innerHTML = date.toString();        }    </script></body>  </html>
Output:
Before clicking on the button:
After clicking on the button:
Approach 2:
Convert given time into date object and store it into new variable date.
Convert the date object’s contents into a string using date.toString() functionExample 2: This example first gets the random milliseconds(1578567991011 ms), Then using that value to get the date by Date() method.<!DOCTYPE HTML><html>  <head>    <title>        JavaScript       | Converting milliseconds to date.    </title></head>  <body style="text-align:center;"      id="body">    <h1 style="color:green;">              GeeksForGeeks          </h1>    <p id="GFG_UP"       style="font-size: 19px;              font-weight: bold;">    </p>    <button onClick="GFG_Fun()">        click here    </button>    <p id="GFG_DOWN"       style="color: green;              font-size: 24px;               font-weight: bold;">    </p>    <script>        var up = document.getElementById('GFG_UP');        var down = document.getElementById('GFG_DOWN');        var milliseconds = 1578567991011;        up.innerHTML = "Milliseconds = " + milliseconds;          function GFG_Fun() {            var date = new Date(milliseconds);            down.innerHTML = date.toString();        }    </script></body>  </html>Output:Before clicking on the button:After clicking on the button:JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.My Personal Notes
arrow_drop_upSave
Example 2: This example first gets the random milliseconds(1578567991011 ms), Then using that value to get the date by Date() method.
<!DOCTYPE HTML><html>  <head>    <title>        JavaScript       | Converting milliseconds to date.    </title></head>  <body style="text-align:center;"      id="body">    <h1 style="color:green;">              GeeksForGeeks          </h1>    <p id="GFG_UP"       style="font-size: 19px;              font-weight: bold;">    </p>    <button onClick="GFG_Fun()">        click here    </button>    <p id="GFG_DOWN"       style="color: green;              font-size: 24px;               font-weight: bold;">    </p>    <script>        var up = document.getElementById('GFG_UP');        var down = document.getElementById('GFG_DOWN');        var milliseconds = 1578567991011;        up.innerHTML = "Milliseconds = " + milliseconds;          function GFG_Fun() {            var date = new Date(milliseconds);            down.innerHTML = date.toString();        }    </script></body>  </html>
Output:
Before clicking on the button:
After clicking on the button:
JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.
arorakashish0911
javascript-date
JavaScript-Misc
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
How to Open URL in New Tab using JavaScript ?
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?