How to Get the Current Timestamp in JavaScript

To get the current timestamp in JavaScript, call the Date.now() function.

For example:

let timestampNow = Date.now()

console.log(timestampNow)

Output:

1657899824770

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 Date.now() function.

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

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()

console.log(timestampNow)

Output:

1657901239710

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 Date.now() 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 Date.now(), 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 = Date.now || function() {
  return +new Date
}

let timestampNow = time()

console.log(timestampNow)

Output:

1657901968575

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())

console.log(timestampNow)

Output:

1657902288149

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()

console.log(timestampNow)

Output:

1657902601214

5. The valueOf() Method

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

For example:

let timestampNow = new Date().valueOf()

console.log(timestampNow)

Output:

1657902713398

Wrap Up

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

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

Some old browsers don’t support Date.now(). Thus, there are some alternatives you might still see. However, these days Date.now() support is such a common thing you should use that!

Thanks for reading.

Happy coding!

Read Also

How to Format Dates in JavaScript