You can use the Date.now()
method to get the UTC timestamp in milliseconds in all modern browsers.
const timestamp = Date.now()
console.log(timestamp)
// 1630735687817
Alternatively, you could also use the getTime()
method or the valueOf()
method on Date
to get the same timestamp:
const timestamp = new Date().getTime()
// OR
const timestamp = new Date().valueOf()
To convert the timestamp to seconds (UNIX time in UTC), you can do the following:
const unixTime = Math.floor(Date.now() / 1000)