How to get the timestamp in JavaScript

Key takeaways:

  • Date.now() provides a quick way to get the current timestamp without creating a new Date instance.

  • getTime() and valueOf() allow us to extract the timestamp from specific date instances, helpful when working with existing date objects.

  • The unary + operator offers a shorthand to retrieve timestamps, giving us flexibility in how we retrieve time values.

  • Retrieving the current timestamp is essential for time-based calculations, scheduling tasks, and tracking event times accurately.

Whether we need to track events, measure durations, or timestamp actions, knowing how to get the current timestamp in JavaScript can significantly enhance our applications. In this Answer, we will learn various methods to obtain the timestamp in JavaScript, including methods like Date.now(), Date.getTime(), valueOf(), and the + operator. Let’s learn together how to do this!

What is a timestamp?

A timestamp is a way to represent a specific point in time, usually in milliseconds since the Unix Epoch (January 1, 1970, 00:00:00 UTC). Timestamps are useful for tracking when an event occurred or measuring intervals between events.

Why do we need timestamps?

Timestamps are crucial for many reasons, including:

  • Tracking events: Knowing when an action occurred can help in logging activities and debugging.

  • Duration measurement: We can calculate the time difference between two events.

  • Scheduling: Timestamps allow us to set and manage time-based tasks.

Let’s dive into how we can obtain the current timestamp in JavaScript.

Methods to get the current timestamp

We can get the current timestamp in JavaScript using several methods. Here are the most common ones:

Method 1: Using the Date.now() method

The simplest way to get the current timestamp is by using the Date.now() method. This method returns the number of milliseconds elapsed since January 1, 1970.

// Getting the current timestamp using Date.now()
let currentTimestamp = Date.now();
console.log("Current Timestamp: " + currentTimestamp);

In the above code:

  • Line 2: We call the Date.now() method to get the current timestamp.

  • Line 3: We print the timestamp to the console.

Method 2: Using the Date.getTime() method

Another way to obtain the current timestamp is by creating a new instance of the Date object and then calling the getTime() method.

// Getting the current timestamp using Date object
let currentDate = new Date();
let timestamp = currentDate.getTime();
console.log("Current Timestamp: " + timestamp);

In the above code:

  • Line 2: We create a new Date object that contains the current date and time.

  • Line 3: We use the getTime() method to retrieve the timestamp in milliseconds.

  • Line 4: We print the timestamp to the console.

Method 3: Using the Date.valueOf() method

The valueOf() method can also be used to get the timestamp from a Date object. It provides the same output as getTime().

// Getting the current timestamp using Date.valueOf()
let currentDate = new Date();
let timestamp = currentDate.valueOf();
console.log("Timestamp using valueOf(): " + timestamp);

In the above code:

  • Line 2: We create a Date object called currentDate, representing the current date and time.

  • Line 3: We call the valueOf() method on the currentDate object to get the timestamp in milliseconds and store it in the variable timestamp.

  • Line 4: We log the timestamp to the console, displaying the milliseconds since the Unix Epoch.

Method 4: Using the unary + operator

The + unary operator is a shorthand way to retrieve a timestamp from a Date object. Internally, it invokes the valueOf() method.

// Getting the current timestamp using the unary + operator
let timestampUsingPlus = +new Date();
console.log("Timestamp using + operator: " + timestampUsingPlus);

In the above code:

  • Line 2: We use the + operator with new Date(), which converts the Date object to its primitive value (a timestamp in milliseconds) and stores it in the variable timestampUsingPlus.

  • Line 3: We log the timestamp to the console, displaying the milliseconds since January 1, 1970.

Getting a timestamp in seconds

If we need the timestamp in seconds instead of milliseconds, we can simply divide the value obtained from Date.now() or getTime() by 1000.

// Getting the current timestamp in seconds
let timestampInSeconds = Math.floor(Date.now() / 1000);
console.log("Current Timestamp in Seconds: " + timestampInSeconds);

In the above code:

  • Line 2: We divide the timestamp obtained from Date.now() by 1000 and use Math.floor() to round down to the nearest whole number.

  • Line 3: We print the timestamp in seconds to the console.

Knowledge test

Let’s attempt a short quiz to assess your understanding.

1

How does the performance.now() method differ from Date.now()?

A)

performance.now() returns a timestamp in seconds.

B)

performance.now() provides a timestamp with microsecond precision.

C)

performance.now() returns the time in UTC.

D)

There is no difference.

Question 1 of 20 attempted

Conclusion

Getting the timestamp in JavaScript is a fundamental task that can help us manage time effectively in our applications. Whether we use Date.now(), the Date object, or + operator, these methods allow us to retrieve the current timestamp in milliseconds or seconds. By understanding how to obtain timestamps, we can enhance our ability to track events, measure durations, and implement time-based functionality in our web applications.


Frequently asked questions

Haven’t found what you were looking for? Contact Us


How to check the timestamp in JavaScript

In JavaScript, we can check the current timestamp by using Date.now(), which returns the current timestamp in milliseconds since January 1, 1970 (UTC). Alternatively, you can create a new Date object and call getTime() to retrieve the timestamp:

console.log(Date.now()); // Current timestamp in milliseconds

How to get the timestamp in milliseconds in JavaScript

To get the timestamp in milliseconds, use Date.now() or new Date().getTime(). Both methods will return the current timestamp in milliseconds:

let timestamp = Date.now(); // or new Date().getTime();
console.log(timestamp);

How to get the UTC timestamp in JavaScript

To get the UTC timestamp, create a Date object and use Date.UTC() with specified year, month, day, etc., for specific dates. If you want the current UTC time in milliseconds, use Date.now() as it provides a timestamp in UTC by default.

let currentUTCTimestamp = Date.now(); // UTC timestamp
let specificUTCTimestamp = Date.UTC(2024, 10, 1); // UTC timestamp for specific date
console.log(currentUTCTimestamp);
console.log(specificUTCTimestamp);

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved