How to get UNIX timestamp from JavaScript Date?

date javascript timestamp unix datetime

Using UNIX timestamp has numerous uses, like cache busting of images or other requests, storing date as integer etc. Timestamp is a number of seconds since UNIX epoch, that is from 1st January 1970.

In JavaScript there is no direct method to obtain seconds based timestamp value, however it can be computed from Date.getTime() function, which return milliseconds instead of seconds.

Getting timestamp in JavaScript:

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

So first we get milliseconds since 1970, then divide by 1000 and round it. Hopefully some day getTimestamp method will be added!