美文网首页
原生日期指定格式化解析

原生日期指定格式化解析

作者: 云桃桃 | 来源:发表于2019-06-16 19:27 被阅读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'));

相关文章

  • 原生日期指定格式化解析

  • Yezi.CMS 标签说明文档

    date(format) 输出当前日期,或使用指定的格式化字符串格式化输出。 参数解析: format 字符串,可...

  • 日期方法

    日期格式化 获取相差指定时间的日期时间戳

  • java DateFormat

    DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。日期/时间格式化...

  • Java-API-包其他

    System Math 时间格式化 DateFormat: 允许进行格式化(也就是日期 -> 文本)、解析(文本-...

  • JavaScript - 时间相关

    基本用法: 获取某月天数 日期格式化 时间格式化 距离现在的时间 time to now 获取从当前时间到指定日期...

  • JS兼容代码及常用代码封装common.js

    //格式化日期======↓// formatDates(dt) //获取指定的标签对象// my$(id)// ...

  • js日期方法

    格式化日期 javascript原生 Date 对象提供了七种格式化方法:1.toString():格式化成 "W...

  • Laravel ORM

    查询指定键名 whereIn 解析能原生SQL是where in()

  • js日期操作

    获取本周第一天 日期相加 日期相减 格式化日期 获取指定月份最后一天

网友评论

      本文标题:原生日期指定格式化解析

      本文链接:https://www.haomeiwen.com/subject/viagfctx.html