JS 时间格式化,模拟PHP date,时间格式化封装函数

JS 时间格式化,模拟PHP date,时间格式化封装函数

Date.prototype.Format = function (fmt) {
    var o = {
        "Y": this.getFullYear(),
        "m": this.getMonth() + 1,
        "d": this.getDate()<10?'0'+this.getDate():this.getDate(),
        "H": this.getHours()<10?'0'+this.getHours():this.getHours(),
        "i": this.getMinutes()<10?'0'+this.getMinutes():this.getMinutes(),
        "s": this.getSeconds()<10?'0'+this.getSeconds():this.getSeconds(),
    };
    for (var k in o){
        if (fmt.indexOf(k) > -1) {
            fmt = fmt.replace(new RegExp("(" + k + ")"),o[k]);
        }
    }
    return fmt;
}

使用方法 (new Date()).Format('Y-m-d H:i:s') 输出 2018-12-07 15:30:00 格式时间