Open In App

JavaScript Converting milliseconds to date

Last Updated : 20 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

To Convert milliseconds to date You can achieve this using the Date object, In this article we will see how to Convert milliseconds to date.

Approach

  • First, declare variable time and store the milliseconds of the current date using the new date() for the current date and getTime() Method for returning it in milliseconds since 1 January 1970.
  • Convert time into a date object and store it into a 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 uses that value to get the date by Date() method

Javascript




let time = new Date().getTime();
function GFG_Fun() {
    let date = new Date(time);
    console.log("Milliseconds = "+ date.toString());
}
GFG_Fun()


Output

Milliseconds = Wed Dec 20 2023 07:22:36 GMT+0000 (Coordinated Universal Time)

Example 2: This example first gets the random milliseconds(1578567991011 ms), Then uses that value to get the date by Date() method.

Javascript




let milliseconds = 1578567991011;
function GFG_Fun() {
    let date = new Date(milliseconds);
    console.log("Milliseconds = " + date.toString());
}
GFG_Fun()


Output

Milliseconds = Thu Jan 09 2020 11:06:31 GMT+0000 (Coordinated Universal Time)

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.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads