JavaScript Dates in SQL

Benjamin Mock - Nov 27 '19 - - Dev Community

SQL and JS dates are not that compatible.

for the datetime type of MySQL for example you need a date in the following format

"2018-04-21 12:11:01"

The suggestions that you usually find on the internet aren't correct, because they don't tate timezones into consideration.

So this does not work correctly!

new Date().toISOString().slice(0, 19).replace('T', ' ');

You can easily solve the problem by using moment.js

require('moment')().format('YYYY-MM-DD HH:mm:ss');

If you don't want to go this route, you can also solve it without an additional library - it just doesn't read as nicely.

const d = new Date(); 
d.toISOString().split('T')[0]+' '+d.toTimeString().split(' ')[0];
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player