JavaScript Date getTime()

Posted by

Method

JavaScript Date getTime() Method

The JavaScript Date.getTime() method is used to return the number of milliseconds since midnight of January 1, 1970, which is also known as the Unix timestamp. This method is often used when working with date and time values in JavaScript.

Syntax

The syntax for the Date.getTime() method is as follows:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

dateObj.getTime()

[/dm_code_snippet]

Where dateObj is a valid Date object.

Parameter Values

This method does not take any parameters.

Return Value

The Date.getTime() method returns the number of milliseconds since midnight of January 1, 1970. This value is often referred to as the “Unix timestamp”.

Example

The following example demonstrates the use of the Date.getTime() method:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var today = new Date();
var time = today.getTime();

console.log(time); //1577760095000

[/dm_code_snippet]

In this example, we create a new Date object called today. We then use the Date.getTime() method to get the number of milliseconds since midnight of January 1, 1970, which is stored in the time variable. Finally, we print out the value of time to the console.

Browser Support

The Date.getTime() method is supported in all modern browsers.