How To Get a Timestamp in JavaScript

How To Get a Timestamp in JavaScript

Learn to use getTime(), now(), and the unary plus operator

Image for postPhoto by Markus Spiske on Unsplash

Working with time is a fundamental skill in any programming language. Some languages have an easier time than others. The latter make what seems like a simple task feel complex enough to get your head spinning.

JavaScript sits in the middle. It at first seems overly complicated; however, it quickly becomes more clear once you understand the pieces in play.

While there?s enough for 10 articles when it comes to JavaScript dates, in this tutorial we?ll go over how to get a timestamp ? both the traditional method and a more concise modern technique.

What Is a Timestamp?

Before we get going, let?s define exactly what a timestamp is. The term is used loosely in a variety of contexts, but we?re going to be defining a timestamp as a numeric representation of a single point in time. Furthermore, our timestamp is going to be the number of milliseconds that have passed from January 1, 1970 UTC?this is known as the Unix epoch.

This means that our timestamp is going to be a long integer, something like 1578054035. What?s the benefit of representing time this way? It certainly is not easier to read ? at least by human eyes. By using a set unit of measure and standard format, it makes time calculations significantly easier because you can perform arithmetic on a Unix timestamp.

Using the Date() Object

When it comes to working with dates and times in JavaScript, the Date() object is your one-stop shop for the majority of needs. The absolute most critical thing to understand is when you create a Date() object, it holds the minutes, hours, day, month, year, etc. as separate pieces of information. You didn?t create a simple representation of time.

For this reason, working with the Date() object can feel overwhelming at first. Fortunately, we don?t need to use all the different features, we just want the timestamp.

There are two traditional methods for getting a timestamp. The first uses the .getTime() method from a Date object.

let d = new Date();let timestamp = d.getTime();console.log(timestamp);

This method is handy when you need to get a timestamp from a specific point in time, rather than the current timestamp.

If you only need the current timestamp, you can bypass the creation of a new object and use the .now() method.

let timestamp = Date.now();

If you want to convert from milliseconds to seconds, we need to do a conversion.

// .getTime()let timestamp = Math.round(new Date().getTime() / 1000);// .now()let timestamp = Math.round(Date.now() / 1000);

If you want to truncate the milliseconds (always round down), then use Math.floor() instead.

Using the Unary Plus Operator

We can further condense the code by leveraging the unary plus operator. A unary operator is one that only takes a single operand (the value that is being operated on). Of the unary operators, the plus operator will attempt to convert its operand into a number.

When a Date() object is used in conjunction with the unary plus operator, we get the unaltered timestamp.

let timestamp = + new Date();console.log(timestamp);

Which method you prefer is largely dependent on your syntactic style and what seems clear to you. For myself, I use the unary plus operator on single statements. However, with multiple statements chained or nested together, I prefer Date.now() because I find it easier to read.

// easy to readlet timestamp = + new Date();// hard to readlet timestamp = Math.floor(+ new Date()/1000);

When I see a unary operator within a chain or within nested statements, my eyes and brain register a missing operand for addition. So, for clarity, I prefer Date.now() in those scenarios.

14

No Responses

Write a response