Format Date to String with String replace method - Node.js Date

Node.js examples for Date:Date Format

Description

Format Date to String with String replace method

Demo Code


Date.prototype.toFormattedString = function (f) {
    var nm = this.getMonthName();
    var nd = this.getDayName();
    f = f.replace(/yyyy/g, this.getFullYear());
    f = f.replace(/yy/g, String(this.getFullYear()).substr(2, 2));
    f = f.replace(/MMM/g, nm.substr(0, 3).toUpperCase());
    f = f.replace(/Mmm/g, nm.substr(0, 3));
    f = f.replace(/MM\*/g, nm.toUpperCase());
    f = f.replace(/Mm\*/g, nm);//from  ww  w.ja va 2s.  c  om
    f = f.replace(/mm/g, String(this.getMonth() + 1).padLeft('0', 2));
    f = f.replace(/DDD/g, nd.substr(0, 3).toUpperCase());
    f = f.replace(/Ddd/g, nd.substr(0, 3));
    f = f.replace(/DD\*/g, nd.toUpperCase());
    f = f.replace(/Dd\*/g, nd);
    f = f.replace(/dd/g, String(this.getDate()).padLeft('0', 2));
    f = f.replace(/d\*/g, this.getDate());
    return f;
};

Related Tutorials