Categories
JavaScript Answers

How to convert JavaScript date time to MySQL datetime?

Spread the love

Sometimes, we want to convert JavaScript date time to MySQL datetime.

In this article, we’ll look at how to convert JavaScript date time to MySQL datetime.

How to convert JavaScript date time to MySQL datetime?

To convert JavaScript date time to MySQL datetime, we can call the date’s toISOString method.

For instance, we write

const date = new Date().toISOString().slice(0, 19).replace("T", " ");

to call toISOString to return a date string of the date in ISO8601 format.

Then we call slice with 0 and 19 to get the datetime part of the string.

And we replace 'T' with a space with replace.

Conclusion

To convert JavaScript date time to MySQL datetime, we can call the date’s toISOString 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 *