现在的位置: 首页 > 综合 > 正文

重写js的Date.format

2018年04月09日 ⁄ 综合 ⁄ 共 620字 ⁄ 字号 评论关闭

/*
格式化日期
format:格式(yyyy-MM-dd hh:mm:ss)
*/
Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //月 
        "d+": this.getDate(), //日
        "h+": this.getHours(), //时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "S": this.getMilliseconds(), //毫秒 
        "q+": Math.floor((this.getMonth() + 3) / 3) //季度 
    }

    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;

抱歉!评论已关闭.