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.