Get timestamp in seconds


Working with timestamps is something rather common. The number of seconds since the 1st of January 1970 are a frequently used way to determine a date in pretty much every programming language. How to do that in JavaScript?


It's fairly simple, but unlike most languages, we don't get seconds straight away, we have to do some math.

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

This will returns the number of seconds in UTC. getTime() returns the timestamp in milliseconds, thus the / 1000.


Please sign-in to post a comment