How to convert milliseconds to date in JavaScript ?

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 miliseconds of current date using new date() for current date and getTime() Method for return it in miliseconds 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:

Approach 2: