i need to send a datetime from javascript to sql in this folowing format: yyyy-mm-dd hh-mi-ss. After creating a variable
var n = new Date()
what should i do to fetch number in that format ? Thank you in advance !!
i need to send a datetime from javascript to sql in this folowing format: yyyy-mm-dd hh-mi-ss. After creating a variable
var n = new Date()
what should i do to fetch number in that format ? Thank you in advance !!
Just use a slightly adapted version from MDN's Date.prototype.toISOString()
polyfill:
if (!Date.prototype.toSQLString) {
(function() {
function pad(number) {
if (number < 10) {
return '0' + number;
}
return number;
}
Date.prototype.toSQLOString = function() {
return this.getUTCFullYear() +
'-' + pad(this.getUTCMonth() + 1) +
'-' + pad(this.getUTCDate()) +
' ' + pad(this.getUTCHours()) +
'-' + pad(this.getUTCMinutes()) +
'-' + pad(this.getUTCSeconds());
};
}());
}
document.write((new Date()).toSQLOString());
Steven Levithan wrote a really nice dateFormat function.
var sqlDateStr = new Date().format("yyyy-mm-dd hh-MM-ss");
console.log(sqlDateStr)