No Time to Read - Copy/Paste Solution

function getCurrentTimestamp () {
return Date.now()
}

Explained

The UNIX timestamp is defined as the number of seconds since January 1, 1970 UTC. In JavaScript, in order to get the current timestamp, you can use Date.now().

It's important to note that Date.now() will return the number of milliseconds since January, 1 1970 UTC. If you need the number of seconds, you'll need to divide the result by 1000.

function getTimestampInSeconds () {
return Math.floor(Date.now() / 1000)
}

The Date class is full of all sorts of handy methods. If you're doing anything with dates in JavaScript, you'll want to use it. You can find the complete API here.


Before you leave

I know, another newsletter pitch - but hear me out. Most JavaScript newsletters are terrible. When's the last time you actually looked forward to getting one? Even worse, when's the last time you actually read one? We wanted to change that.

We call it Bytes, but others call it their favorite newsletter.

Delivered to 111,962 developers every Monday and Thursday