Categories
JavaScript Answers

How to convert normal date to Unix timestamp with JavaScript?

Spread the love

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

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

How to convert normal date to Unix timestamp with JavaScript?

To convert normal date to Unix timestamp with JavaScript, we can use the getTime method.

For instance, we write

const d = "2022-01-01T00:00:00.000Z";
console.log(new Date(d).getTime());

to create the Date object from datetime string d.

And then we call the getTime method to get the Unix timestamp in milliseconds.

Conclusion

To convert normal date to Unix timestamp with JavaScript, we can 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 *