I have been exploring Nodejs for past weeks. I can say that it uses different technique that I used to know from other programming languages. I have a little experience with javascript programming as well as the nodejs. And Nodejs encourage me to explore more on javascript. My created application involves with the Nodejs and MySQL database, especially the datetime datatype. This javascript code comes very handy and easily converts javascript date to MySQL datetime.
function twoDigits(d) {
if(0 <= d && d < 10) return "0" + d.toString();
if(-10 < d && d < 0) return "-0" + (-1*d).toString();
return d.toString();
}
Date.prototype.toMysqlFormat = function() {
return this.getUTCFullYear() + "-" + twoDigits(1 + this.getUTCMonth()) + "-" + twoDigits(this.getUTCDate()) + " " + twoDigits(this.getHours()) + ":" + twoDigits(this.getUTCMinutes()) + ":" + twoDigits(this.getUTCSeconds());
};
var MyDate = new Date();
MyDate.toMysqlFormat(); //return MySQL Datetime format
Hope it helps you as it helped me.
Don’t use this, buggy.
2012-02-01 01:40:21 (in JS Date) converted to 2012-01-31 01:40:21
Tried to use it and was getting the problem (before getting to the comment section).
getHours should be getUTCHours, as the other calls. Or no UTC version should be called.