Home  »  ArticlesProgrammingSoftwareTechnologyTips   »   Here are Three Subtle Ways to get a Unix Timestamp in JavaScript

Here are Three Subtle Ways to get a Unix Timestamp in JavaScript

Did you know you can “easily” get a Unix-like timestamp using JavaScript in more than just one way? Everyone has their own reasons as to why they would want to do this, but hey! We are not Judging.

For those who are lost at this point, a Unix timestamp which is also known as POSIX time or epoch time is a system for describing instants in time, or so says Wikipedia. It is the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970.

This system is widely used on Unix-like operating systems that is why is commonly called a Unix timestamp. It is also important to note that this system does not factor in leap seconds and therefore is not a true representation of UTC.

To print the time stamp on Unix-like operating systems you can simply type this date +%s in the command line. Now let us see how we can do this in JavaScript as we restrict this to Three examples.

How to get the Unix Timestamp in JavaScript

Method 1: If you call:

new Date();

You get a formatted date representation that looks similar to this:

Wed Mar 01, 2017, 19:56:30 GMT+0300 (E. Africa Standard Time)

That, unfortunately, is not a timestamp. We can take advantage of a side effect of the JavaScript language and force that call to give us the timestamp by prefixing the + unary operator which triggers the valueof method in that Date object. Using this method may not be considered good coding practice but it works nevertheless. With that said, therefore to get the timestamp you can call:

new Date();

or alternatively or more correctly:

new Date().valueOf();

new Date().valueOf()

To get seconds from milliseconds you can do the following.

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

Method 2: You can get the timestamp using the now() method of the Date object as shown below.

Date.now();

Older browsers (read IE8 and older) will not be able to use this method without a shim such as the one shown here.

if (!Date.now) {
 Date.now = function() { return new Date().getTime(); }
 }

Now, this displays the UTC timestamp in milliseconds. should you want to display the above in seconds you can process it further as seen here.

Math.floor(Date.now() / 1000);

Method 3: As a side effect of the above method, you will notice there is a third way of doing this without creating another date object. it is simply to call the getTime method as shown here.

new Date().getTime();

To retrieve the seconds from the above you can do this:

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

There you have it. That is the basics of getting the Unix-like UTC timestamp in JavaScript, what you do with it, the possibilities are limitless.

See also  How to Enable or Disable HTML Form Elements Using jQuery

Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.

Share it!