Categories
JavaScript Answers

How to convert string to datetime with JavaScript?

Spread the love

Sometimes, we want to convert string to datetime with JavaScript.

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

How to convert string to datetime with JavaScript?

To convert string to datetime with JavaScript, we use the Date constructor.

For instance, we write

const s = "01-01-2022 00:03:44";
const d = new Date(s);
console.log(d);

to call the Date constructor with string to return a Date object converted from the string.

Conclusion

To convert string to datetime with JavaScript, we use the Date constructor.

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 *