Categories
JavaScript Answers

How to generate timestamp unix epoch format with Node.js?

Spread the love

To generate timestamp unix epoch format with Node.js, we use the Date constructor.

For instance, we write

const ts = Math.floor(new Date().getTime() / 1000);

to create a new date with the current date and time with the Date constructor.

Then we call getTime to get its timestamp in milliseconds.

And then we divide that by 1000 to get the timestamp in seconds.

Finally, we call Math.floor with the timestamp in seconds to round it down to the nearest integer.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *