Format Date yyyymmdd hhmmss - Javascript

Format Date yyyymmdd hhmmss - Javascript

code snippet

javascript
Date.prototype.yyyymmddhhmmss = function () {
    var mm = this.getMonth() + 1; // getMonth() is zero-based
    var dd = this.getDate();
    var hh = this.getHours();
    var mi = this.getMinutes();
    var ss = this.getSeconds();
    var yyyymmdd = [this.getFullYear(),
            (mm > 9 ? '' : '0') + mm,
            (dd > 9 ? '' : '0') + dd
    ].join('-');

    var hhmmss = [(hh > 9 ? '' : '0') + hh,
     (mi > 9 ? '' : '0') + mi,
     (ss > 9 ? '' : '0') + ss, ].join(':');

    return yyyymmdd + ' ' + hhmmss;
};

// to use it
new Date().yyyymmddhhmmss();

"Wonder rather than doubt is the root of all knowledge"

Contact me!

Have questions? Feel free to use the form or send me an email with any questions.

I will be responsive to your requests and your questions.