Categories
JavaScript Answers

How to convert a datetime string to timestamp in JavaScript?

Spread the love

Sometimes, we want to convert a datetime string to timestamp in JavaScript.

In this article, we’ll look at how to convert a datetime string to timestamp in JavaScript.

How to convert a datetime string to timestamp in JavaScript?

To convert a datetime string to timestamp in JavaScript, we can call Date.parse.

For instance, we write:

const s = '2022-01-01'
const timeInMillis = Date.parse(s);
console.log(timeInMillis)

to call Date.parse with s.

Then we get that timeInMillis is 1640995200000.

Conclusion

To convert a datetime string to timestamp in JavaScript, we can call Date.parse.

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 *