原生日期指定格式化解析

2019-06-16  本文已影响0人  云桃桃
image.png
    Date.prototype.format=function (type) {
    // 处理 月日 时 分 秒 毫秒
    var obj={
      'M+':this.getMonth()+1,
      'D+':this.getDate(),
      'h+':this.getHours(),
      'm+':this.getMinutes(),
      's+':this.getSeconds(),
      'S':this.getMilliseconds()
    };
    var weeks=['日','一','二','三','四','五','六','七'];
    // 这主要就是一个替换,把format后面的值根据位数替换为指定日期的值
    // 处理年
        var yearReg=/(Y+)/g;
        if(yearReg.test(type)) {
            type = type.replace(RegExp.$1, (this.getFullYear()+'').substr(4-RegExp.$1.length));
        }
    // 处理周
        var weekReg=/(E+)/g;
        if(weekReg.test(type)){
            type=type.replace(RegExp.$1,'星期'+weeks[this.getDay()]);
        }
// 处理 两位、一位数的 
    for(var k in obj){
        var reg=new RegExp('('+k+')','g');
        if(reg.test(type)){
            // 如果指定格式是1位,原值输出;如果是两位的 则前面加0处理
            type=type.replace(RegExp.$1,RegExp.$1.length==1?obj[k]:('0'+obj[k]).slice((''+obj[k]).length-1));
        }
    }
    return type;
    };
    console.log(new Date('2018-12-5 12:00:00').format('YYYY-MM-DD EE hh:mm:ss'));
    console.log(new Date('2018-12-5 12:30:00').format('YYYY-MM-DD EE hh:mm'));
    console.log(new Date('2018-12-5 12:3:00:23').format('YY-MM-D EE h:m:s:S'));
上一篇 下一篇

猜你喜欢

热点阅读