Register Login

getTime() to generate Timestamps in JavaScript

Updated Mar 01, 2024

The getTime() is a method in JavaScript that returns a total time in milliseconds since the epoch. This time count represents the midnight at the start of 1st January 1970, UTC. Users can use this JS method to assign any date and time object from the current data object.

The JS Date.now() method with the getTime() method is the most popular method among users and programmers. This article will highlight techniques to use and generate timestamps using getTime().

What is the getTime() method in JavaScript?

The getTime() method returns a numeric value according to the universal time that counts the number of milliseconds since the Unix time. This numeric value corresponds to the time for the specified date, so users can use this method to generate timestamps in JavaScript. In the following code instance, we will use the getTime() method and the JS Date.now() method to create an object of the date class and generate a timestamp.

What is a timestamp?

The timestamp is a format of getting the time and date using the default methods of JavaScript. It is a way to represent the date and hold the time in readable formats by creating new datetime objects.

Several methods, like valueof(), the JS Date(), getTime(), etc., use this date object to retrieve the count of milliseconds that have passed since the Unix time or 1st January 1970, UTC. In the following sections, we will discuss the default date() and getTime() methods and the two built-in JS methods to manage the dates and times.

Example 1: Using date() and getTime() method to print timestemp in miliseconds

Code Snippet:

<html>
<head>
<title>How to use the getTime() to generate Timestamps in JS?</title>
</head>
<body>
<p> The count of the internal clock in JavaScript begins at midnight 1st January 1970. </p>
<p> The getTime() methods returns the number of milliseconds since 1st January 1970: </p>
<p id = "sample"></p>
<script>
	const d = new Date();
	
	//The getTime() methods returns the number of milliseconds since 1st January 1970
	let time = d.getTime();
	
	// Print the timestemp in miliseconds
	document.getElementById("sample").innerHTML = time;
</script>
</body>
</html>

Output:

Run Code

Explanation:

We used the date() and getTime() methods to generate the timestamp in the above code. The getTime() method returns the milliseconds since the epoch.

The date() method returns the date object and counts the time from the specified date and time. The getTime() returned "1680850588027," the time in milliseconds.

Example 2: Using date() and getTime()

Code Snippet:

<html>
<head>
<title>How to use the getTime() to generate Timestamps in JS?</title>
</head>
<body>
<h1>Example of date() and getTime() methods</h1>
<script>
	var y = d * 365;
	var m = 1000 * 60;
	var h = m * 60;
	var d = h * 24;
	
	var date = new Date();
	var time = date.getTime();
	var res = Math.round(time / y);
	
	var t= Date.now();
	document.write("<hr>" + time + "<hr>");
	var d = new Date(t);
	document.write(d);
</script>
</body>
</html>

Output:

Run Code

Explanation:

We used the date() and getTime() methods to generate the timestamp in the above code snippet. Then, in the JavaScript code, we used the four variables specifying the year, month, hour, and day inside the function sample.

The variables "y," "m," "h," and "d" specifies the year (multiplied by 365 days), minute (multiplied by 60), hour (multiplied by 60), and day (multiplied by 24). The Math.round() method returns the resultant time rounded to the nearest integer value.

The date() method returns the date object and counts the time from the specified date and time. The getTime() returned "1680852980488," the time in milliseconds.

Example 3: Get date and time in seconds using new Date().valueOf()

We will fetch the time and date in seconds using the new Date().valueOf(). It gets the number of seconds from the JS epoch.

Code Snippet:

<html>
<head>
<title>How to use the getTime() to generate Timestamps in JS?</title>
</head>
<body>
<h1>Get date and time in seconds using new Date().valueOf()</h1>
<script>
	console.log(Math.floor(new Date().valueOf()/1000));
	document.write(Math.floor(new Date().valueOf()/1000) + "<hr>");
	
	console.log(Math.floor(new Date().getTime()/1000));
	document.write(Math.floor(new Date().getTime()/1000) + "<hr>");
	
	console.log(Math.floor(Date.now()/1000));
	document.write(Math.floor(Date.now()/1000) + "<hr>");
</script>
</body>
</html>

Output:

Run Code

Explanation:

In the example, we have returned the time in seconds using the new Date.valueOf() method. The valueOf() method returns the primitive value of the Date object.

Example 4: Timestamp in milliseconds using now() method to get high resolution

Code Snippet:

<html>
<head>
<title>How to use the getTime() to generate Timestamps in JS?</title>
</head>

<body>
	<script>
		var demo = (
			window.performance &&
			window.performance.now &&
			window.performance.timing &&
			window.performance.timing.navigationStart
		);

		var time_stamp = (
			demo ?
			window.performance.now() +
			window.performance.timing.navigationStart :
			Date.now()
		);

		document.write(time_stamp, Date.now());
</script>
</body>
</html>

Output:

Run Code

Explanation:

We have used the performance.now() method to get a high-resolution timestamp in milliseconds. The method illustrates the time elapsed since performance.timeOrigin. It represents the time navigation started in window contexts or when the worker runs in ServiceWorker and Worker contexts.

Example 5: Unix Timestamp

<html>
<head>
<title>How to use the getTime() to generate Timestamps in JS?</title>
</head>

<body>
	<script>
		// returning the current time in Unix format using Date() object 
		var u = Math.round(+new Date() / 1000);
		console.log(u);
		document.write('Current time in Unix format: ' + u);
		
		// returning the current time in milliseconds 
		var ms = new Date().getTime();
		console.log(ms)
		document.write('<br>Current time in milliseconds:' + ms);
	</script>
</body>
</html>

Output:

Run Code

Explanation:

Here, we have used two distinct approaches one that will return the Unix time in seconds and the other that will only return the time in milliseconds, not in Unix time. The first output is in Unix timestamp, and the latter is only in milliseconds.  

Browsers the support getTime() method:

  • Chrome
  • Firefox
  • Opera
  • Edge
  • Safari

Conclusion:

Other methods like valueof(), the JS Date.now() to retrieve the timestamp in JavaScript, but since this getTime() method is a well-chosen method; this article highlighted how it works and the code examples to get the timestamp using this method.

In the output console, when users press the button or execute the program, each time, they will get a new timestamp that depicts the updated time that changes in a millisecond, typically associated with the Unix time.


×