JavaScript

JavaScript Date getTime() Method

The JavaScript Date.getTime() method extracts the numerical number in milliseconds since January 1st, 1970. The method always utilizes the Universal Time Coordinated (UTC) format to represent the time.

If you are not familiar with the getTime() method in JavaScript, it is the best place to learn and understand this method. We have compiled this guide to provide a brief overview of the working and usage of the Date.getTime() method.

How to Use the Date getTime() Method in JavaScript?

The getTime() returns the milliseconds based on the local time for a specified date. Users can use the date object to call the getTime() method in JavaScript. Moreover, users can utilize it to assign the time and date to another date object in JavaScript.

The syntax of the getTime() method in JavaScript is as follows:

Syntax

Date.getTime()

 
The Date is the object that saves information about the time and date. The getTime() method will not accept parameters and returns the integer value, which is equal to the number of milliseconds.

Example 1: Retrieve the Time From a Specific Date/Time

In the example code, we are using the Date.getTime() method to retrieve the time from the user-defined time in milliseconds.

Code

<script>
    // An example for using the getTime() method
    // While creating Date object
    var date1 = new Date('December 24, 2000 04:25:52');
    // The information for milliseconds is
    // extracted using getTime()
    var date2 = date1.getTime();
    // Display time in milliseconds.
    document.write(date2);
</script>

 
In the above code:

    • The date1 is the variable that stores the date/time information.
    • After that, the getTime() method is used to extract the milliseconds that are stored in the date1 variable. The extracted time is stored in the date2 object.
    • Finally, the value stored in the date2 object is displayed using the document.write() method.


Output


After executing the code, the display represents the output in milliseconds using the getTime() method in JavaScript.

Example 2: To Get the Time From User Defined getTime() Method

In this example, the date is taken from the user and then the getTime() method will be applied to it. Let’s see how it works:

Code

<script>
    // An example for using the getTime() method
    // While creating Date object

    const input = prompt("What's your Birthday Date?");
    alert(`Your Birthday Date is ${input}`);
    var date1 = new Date(input);
    // The information for milliseconds is
    // extracted using getTime()
    var date2 = date1.getTime();
    // Display time in milliseconds.
    document.write(date2);
 </script>

 
The description of the above code is enlisted here:

    • Firstly, store the date of birth in a variable named input.
    • After that, an alert box is generated for that prompt.
    • Extract the information every millisecond using the getTime() method.
    • Finally, display the information using the date2 variable.


Output

Upon executing the code, the following interface is observed:


After the execution of the code, the user must input the date of birth, as we entered March 10, 1994. Click on OK to proceed further:


A prompt message is generated for confirmation of the data provided through the user input:


In the end, the milliseconds that have passed since January 1st, 1970, are displayed.

Conclusion

JavaScript is useful for extracting information in milliseconds using the Date.getTime() method. The date object is used to call this method, and it retrieves the values in milliseconds that have passed since January 1st, 1970. This method’s output can be obtained on various modern browsers such as Firefox, Safari, Edge, and Opera. You have learned the working of the getTime() method in JavaScript.

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.