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.
<!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
-
Before clicking on the button:
-
After clicking on the button:
- JavaScript | Adding days in milliseconds to Date object
- How to convert a JavaScript date to UTC?
- Convert string into date using JavaScript
- JavaScript | Date.now()
- JavaScript | Date
- Date.UTC( ) In JavaScript
- JavaScript | Date Formats
- JavaScript | date.setUTCMonth()
- JavaScript | date.toLocaleString()
- JavaScript | date.toLocaleDateString()
- JavaScript | Date.parse()
- JavaScript | Set Date Methods
- How to get the current date in JavaScript ?
- JavaScript | date.toLocaleTimeString()
- JavaScript | Date Objects
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:
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



