JavaScript Date getTime() Method

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

We use the getTime() method in Javascript to get information about a given date. Let us consider that we have a specific date. The Javascript getTime() will give us the exact number of milliseconds passed between the ECMAScript epoch, i.e January 1, 1970, UTC, and the specified date. This value is the numeral value for the specified date according to Universal Time. If we provide a date lesser than Jan 1, 1970, Javascript getTime() will return a negative value. Using the Javascript getTime() method, we can assign a date and time to any Date object in Javascript.

Syntax of JavaScript Date getTime() Method

Let us now see the syntax for the javascript getTime() Method.

The Date object in Javascript stores information about time and date. A Javascript date is the number of milliseconds that have passed since Jan 1'1970 or the ECMAScript epoch. We can create a date variable with any date and then use the getTime() method on it to retrieve information about the given date.

Parameters of JavaScript Date getTime() Method

The Javascript getTime() method does not accept any parameter.

Return Value of JavaScript Date getTime() Method

The return value of the Javascript getTime() Method is the number of milliseconds passed between January 1, 1970, UTC, and the specified date. This is also the numeral value for the given date in universal time.

Exceptions of JavaScript Date getTime() Method

The Javascript getTime() method will return a negative value if we provide it with a date before Jan 1'1970 UTC.

Example

Let us go through an example of the Javascript getTime() method.

In the following example, we are required to count the number of days since EcmaScript Epoch.

We know that using the getTime() method on the current date gives the number of milliseconds that have elapsed since the EcmaScript Epoch. Now, we can easily get the number of days that have elapsed, by dividing the obtained figure by the number of milliseconds in a day.

No of days since Jan 1 '1972 = No of milliseconds since Jan 1 '1972 / No of milliseconds in a day

Code

Output

Explanation

  • At first we calculate the number of milliseconds in a day and store it in the variable named 'day'.
  • We obtain the current date by using the Date object in Javascript.
  • We get the number of days elapsed by dividing this Date object variable by 'day'.

What is JavaScript Date getTime() Method?

The getTime() method is used with the Date object in Javascript to obtain information about a specific date. Before exploring it further let us first have a brief discussion on what are methods and objects in Javascript.

Everything is an object in Javascript. It includes booleans, numbers, strings, dates, maths, regular expressions, arrays, functions, and other objects. Methods are actions that we can perform on these objects. They are used to perform some tasks and are stored as object properties.

A date is an object in Javascript that is used to represent a moment in time. The Date object contains the number of seconds that have passed since Jan 1 1970 UTC or the ECMAScript epoch.

Different methods are available to us to get the Date in different formats. These include getTime(), getSeconds(), getMinutes(), getYear(), getUTCDay() etc. Out of these, the Javascript getTime() method helps to denote the numerical value of the date, i.e the number of milliseconds that have passed from the ECMAScript epoch till the given date. This method does not take in any parameter and returns the number of milliseconds as the return value.

There are a lot of applications of the JavaScript getTime() method:

  • Along with the setTime() method, it is used to assign some date and time to another Date object in Javascript.
  • It can be used to measure the execution time of a function.

We shall cover the code for the above applications in the following sections.

More Examples

In this section we shall cover the possible applications for Javascript getTime() method with the help of the following examples:

Example 1:

In the following example, we are required to use the getTime() method to assign a date to another date variable.

As discussed above, the Javascript getTime() method can be used with the Javascript setTime() method to assign a date to another date variable. The setTime method in Javascript takes in a time value (in milliseconds) and sets a date object to the equivalent time since ECMAScript Epoch as represented by the time value. We can use the getTime() method on a specific date to get the time value, which is the number of milliseconds that have passed between the EcmaScript Epoch and the given date. We can then use the Javascript setTime method on a new date with the created time value.

Let us see the code for the same.

Code:

Output:

Explanation:

  • At first we are storing a specific date by passing the exact date in the new Date() constructor.
  • Then we are creating a new date to which we shall assign the previous date.
  • We are checking the new date before the assignment is done. It should return the current date.
  • We are assigning the previous date to the new date by using Javascript setTime and Javascript getTime methods.
  • Then we are checking the new date which should reflect the exact date we had created in the beginning.

Example 2:

In this example, we are required to use the getTime() method to calculate the execution time of a function. We do this by creating two date objects before and after our function call is executed. We then get the difference in milliseconds between them by using Javascript getTime() method. This difference is the time that our function call has taken to execute.

Let us now see the code for the same.

Code:

Output:

Explanation:

  • In the above code, we have seen the execution time for the sqrt function which has been called 10000000 times.
  • For this, at first we are storing the start time before starting the function call.
  • Then we are executing our sqrt function which we are calling 10000000 times.
  • Then we are storing the end time.
  • The difference between the start time and end time gives the number of milliseconds that the code block/function call has taken to execute.

Supported Browsers

Javascript getTime() method is compatible with the following browsers:

  • Chrome
  • Edge
  • Firefox
  • Opera
  • Safari
  • Chrome Android
  • Firefox for Android
  • Opera for Android
  • Safari on IOS
  • Webview Android
  • Samsung Internet
  • Deno

Conclusion

  • The Javascript getTime() method is used to obtain information about a date variable.
  • It specifies the number of milliseconds that have passed between Jan 1 '1970 and the specified date.
  • This value gives us the numeral value for a specified date according to Universal Time.
  • It is a feature of ES1 and is compatible with all major browsers, including Chrome, Edge, Firefox, Opera, Safari, etc.
  • It has applications in retrieving the execution time of function calls.
  • Using the Javascript getTime() method, we can assign a date and time to any Date object in Javascript.