Categories
JavaScript Answers

How to convert date and time to Unix timestamp with JavaScript?

Spread the love

Sometimes, we want to convert date and time to Unix timestamp with JavaScript.

In this article, we’ll look at how to convert date and time to Unix timestamp with JavaScript.

How to convert date and time to Unix timestamp with JavaScript?

To convert date and time to Unix timestamp with JavaScript, we use the getTime method.

For instance, we write

const unixtime = Date.parse("24-Nov-2022 17:57:35").getTime() / 1000;

to parse the datetime string with Date.parse.

Then we call getTime to return the Unix timestamp in milliseconds.

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

Conclusion

To convert date and time to Unix timestamp with JavaScript, we use the getTime method.

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 *