Convert JavaScript to date object to MySQL date format (YYYY-MM-DD)

To get the date:

const date = new Date().toJSON().slice(0, 10)
console.log(date) //2015-07-23

For datetime:

const datetime = new Date().toJSON().slice(0, 19).replace('T', ' ')
console.log(datetime) //2015-07-23 11:26:00

Note that the resulting date/datetime will always be in the UTC timezone.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)