How to Get the Current Timestamp in JavaScript (5 Ways)

To get the current timestamp in JavaScript, call the function.

For example:

let timestampNow =




If that’s what you were looking for, then thanks for reading!

If you are interested, the following sections teach you some insight into the significance of timestamps.

You will also learn alternative ways to obtain the current timestamp in JavaScript.

What Is the Significance of the Current Timestamp?

The current timestamp is also called Epoch time or UNIX timestamp. In computer science, the current timestamp is the number of seconds since January 1, 1970.

The current timestamp is the same no matter where you are on the globe.

But why January 1, 1970?

To make a computer track time, it has to know where to start. During the development of the UNIX system, Jan 1, 1970 became the beginning of time for UNIX-like systems.

So the current timestamp represents how many seconds have elapsed since Jan 1, 1970.

Commonly, you’ll see these epoch times in databases. There are many reasons why epoch timestamps make sense:

  1. Epoch timestamps are lightweight. They make your database perform better as they eat up less memory. This truly matters when there are millions of entries in the DB.
  2. Before formatting the date, a typical server-side language converts the timestamp to an integer anyways. Thus, it makes sense for the timestamp to be an integer, to begin with.
  3. There are no timezone dependencies. You only need to know the timezone to convert the timestamp to.

Current Timestamp in JavaScript: Alternative Ways

In JavaScript, the current timestamp is expressed in milliseconds. As you learned in the intro, the de-facto way to obtain the timestamp is the function.

The reason why you sometimes see alternative approaches is that not all browsers supported in the past. These days, almost all browsers do

1. Unary Operator “Trick”

You can use the unary + operator to get the current timestamp from a Date object.

+ new Date()

For example:

let timestampNow = + new Date()




This works because the preceding unary + operator causes the valueOf method of the Date object.

2. Support Old Browsers

As stated earlier, older browsers didn’t support the function. If this was the case, then a quick way to obtain the timestamp is the previously learned unary operator trick: + new Date().

But if the browser supports, then it makes sense to use that.

Here is a function that uses the latter if it’s applicable and the former if not:

var time = || function() {
  return +new Date

let timestampNow = time()




3. Convert the Date to Number

You can call the Number() function on a new Date object. This converts the object to an integer that represents the current timestamp.

For example:

let timestampNow = Number(new Date())




4. The getTime() Method

Last but not least, you can get the current timestamp in milliseconds using the getTime() method of a Date object.

For example:

let timestampNow = new Date().getTime()




5. The valueOf() Method

Given a Date object, the valueOf() method returns its timestamp.

For example:

let timestampNow = new Date().valueOf()




Wrap Up

Today you learned how to get the current timestamp in JavaScript.

To take home, use the function to get the timestamp. This function returns the current timestamp in milliseconds.

Some old browsers don’t support Thus, there are some alternatives you might still see.

However, these days support is such a common thing you should use that!

Scroll to Top