Skip to content
Related Articles

Related Articles

Improve Article

JavaScript | Converting milliseconds to date.

  • Last Updated : 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() function

    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.




    My Personal Notes arrow_drop_up
Recommended Articles
Page :