Any DateTime Javascript function returns MySQL datetime format?

I have a function that sends an ajax request to a server file with parameters. The problem is that I want to send the current DateTime value to compare it with the database.

as the MySQL datatime format, all dates are dated as the format "2012-02-03 19:50:28".

How can I generate it in Javascript.

Another question: can I add the clock to the current time and time (to fix the problem with the server’s time zone)

Thanks in advance

+4
source share
3 answers

try it

/* use a function for the exact format desired... */ function ISODateString(d){ function pad(n){return n<10 ? '0'+n : n} return d.getUTCFullYear()+'-' + pad(d.getUTCMonth()+1)+'-' + pad(d.getUTCDate()) +' ' + pad(d.getUTCHours())+':' + pad(d.getUTCMinutes())+':' + pad(d.getUTCSeconds()) } var d = new Date(); console.log(ISODateString(d)); // prints something like 2009-09-28 19:03:12 

Reference:
date format
javascript date type
works with dates

+9
source

Try the following:

 Date.prototype.toMysqlFormat = function () { function pad(n) { return n < 10 ? '0' + n : n } return this.getFullYear() + "-" + pad(1 + this.getMonth()) + "-" + pad(this.getDate()) + " " + pad(this.getHours()) + ":" + pad(this.getMinutes()) + ":" + pad(this.getSeconds()); }; var TimeNow = new Date().toMysqlFormat(); alert(TimeNow); //Alerts Current TimeStamp 
+4
source

Enter the date in the format string, you can do it using the Date API or use Datejs (a powerful date plugin).

But I recommend that you push the millisecond number to the / mysql server instead of the line:

 new Date().getTime(); 
+1
source

Source: https://habr.com/ru/post/1394735/


All Articles