格式化时间字符串的万能公式

        在项目中经常会遇到对于时间字符串进行格式化的要求,正常而言有两种方法对时间字符串进行格式化处理,一种是通过处理字符串进行格式化,一种是通过处理日期对象来进行格式化,如下(末尾提供处理格式化时间字符串的万能公式)

第一种,通过处理字符串的方式来将时间字符串进行格式化:


//格式化时间字符串的处理方法1 - 字符串处理方法
    let time = "2022-2-18 17:20:15"
    

    /*
      formatTime: 通过字符串格式化时间字符串
        @params: 
          time 需要处理的时间
        @return:
          格式化后的时间字符串
      by Zack on 2022年2月18日
    */
    function formatTime(time){
      //先获取时间信息
      let arr = time.split(' ')
          arrLeft = arr[0].split('-')
          arrRight = arr[1].split(":")
          arr = arrLeft.concat(arrRight);
      //按照需求更改这里的格式 : "年月日,时分秒"
      let result = arr[0] + "年" + addZero(arr[1]) + "月" + addZero(arr[2]) + "日 ";
      result += addZero(arr[3]) + "时" + addZero(arr[4]) + "分" + addZero(arr[5]) + "秒";
      console.log(result);  
      return result
    }

    function addZero(val){
      val = Number(val)
      return val < 10 ? '0' + val: val
    }

    formatTime(time)//=>2022年02月18日 17时20分15秒

        使用这种方式处理时间字符串的话并不推荐,因为如果传入的time 需求改变为不需要精确到时分秒,那么在处理arrRight这行的时候就会报错,需要修改代码。

第二种,通过处理日期对象格式的方式来对时间字符串进行格式化:

//处理方法2 - 基于日期对象处理
let time = "2022-2-18 17:20:15"
    function formatTime2(time) {
      //1.把时间字符串变成标准日期对象
      time = time.replace(/-/g,'/')
      time = new Date(time)
      //2.获得小时分钟秒
      let year = time.getFullYear(),
          month = addZero(time.getMonth() + 1)
          day = addZero(time.getDate())
          hours = addZero(time.getHours())
          minutes = addZero(time.getMinutes())
          seconds = addZero(time.getSeconds())

      console.log(year + '年' + month + "月" + day + "日" + " " + hours + "时" + minutes + "分" + seconds + "秒"); 
      return year + '年' + month + "月" + day + "日" + " " + hours + "时" + minutes + "分" + seconds + "秒"

    }
    formatTime2(time)//=>2022年02月18日 17时20分15秒

        第二种方法相对于第一种而言更加具备容错性,但是如果需求修改成不需要显示年和秒,只显示 月日时分 那么也是要修改代码的。所以,有了第三种万能公式-来自周啸天老师的分享

第三种,万能公式:


    //封装一套公共的时间字符串格式化处理的方式:(超牛的万能格式化)
    String.prototype.formatTime = function formatTime(template) {
      //初始化模板
      typeof template === 'undefined' ? template = "{0}年{1}月{2}日 {3}时{4}分{5}秒" : null;
      //this:我们要处理的字符串
      //获取字符串中的数字信息
      let matchArr = this.match(/\d+/g)
      //模板和数据的渲染(引擎机制)
      template = template.replace(/\{(\d+)\}/g, (x, y) => {
        let val = matchArr[y] || "00";
        val.length < 2 ? val = '0' + val : null
        return val;
      })
      return template;
    }
    let time = "2022-2-18 18:12"
    console.log(time.formatTime());//2022年02月18日 18时12分00秒
    console.log(time.formatTime("{1}/{2} {3}:{4}"));//02/18 18:12
    console.log(time.formatTime("{0}/{1}/{2}"));//2022/02/18

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值