How to convert a datetime string to timestamp in JavaScript?

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.