Vanilla JavaScript get current timestamp

β€” 3 minute read

permalink

As developers, we use timestamps a lot, for storing when last a function was called, but even too safe dates for specific actions.

The Unix epoch (timestamp) started 1st January 1970 at 00:00:00 and ticks every second, so the number you see is the number of seconds since that moment.

In case you are wondering, yes there is a limit to this method and it will become obsolete in 2038. (Since we run out of space to store the seconds in this 32-bit integer).

As for now, it's still a vastly used method, you might have to store or compare to something that was previously set.

Ways to get the timestamp in JavaScript permalink

In JavaScript, we have multiple ways of getting the current timestamp.

This one is my favorite, since it's the most readable one, and it works on every browser.

new Date().getTime();

Another version of this:

new Date().valueOf();

If you don't have to support the older browser you can also use:

Date.now();

All of these will return a timestamp like this:

// 1610341839403

For those familiar with a timestamp, you might have noted this is longer than what you might be used to. That is because JavaScript gives us the timestamp in milliseconds.

That means we need to divide the number by 1000

Math.floor(new Date().getTime() / 1000);

Now we will get:

// 1610341839

You can check these timestamps on epochconverter.com.

Converting a timestamp into a date permalink

Perhaps you are wondering how to convert this timestamp back into a date?

We can simply pass the timestamp to the date object.

new Date(1610341839403);

We can then convert that in a string for instance:

new Date(1610341839403).toString();
// 'Mon Jan 11 2021 07:10:39 GMT+0200 (South Africa Standard Time)'

And there you go, you just learned how to use a timestamp in JavaScript!

Thank you for reading, and let's connect! permalink

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter