How to get a timestamp in JavaScript
When working with dates and times it's often useful to be able to represent your point in time as a single number. A good example of this in practice is Unix Timestamps, where it is represented as the number of seconds that have passed since the Unix epoch; 00:00:00 UTC on 1 January 1970.
In JavaScript we can achieve something similar by leveraging the native Date object. By using Date.now()
we can get the UTC timestamp in milliseconds.
console.log(Date.now());
// 1595331292901
It's also possible to use this short hand that uses a unary operator to trigger the valueOf method on our Date object.
console.log(+new Date());
// 1595331292901
If you wanted to then convert these to seconds to match the Unix Epoch Time you would just do this:
Math.floor(Date.now() / 1000);
// 1595331292