阅读 639

vue JS获取当前时间并格式化 ( 前端小细节,大进步 )

JS

// 获取并且格式化时间
    formatter (thistime, fmt) {
      let $this = new Date(thistime)
      let o = {
        ‘M+‘: $this.getMonth() + 1,
        ‘d+‘: $this.getDate(),
        ‘h+‘: $this.getHours(),
        ‘m+‘: $this.getMinutes(),
        ‘s+‘: $this.getSeconds(),
        ‘q+‘: Math.floor(($this.getMonth() + 3) / 3),
        ‘S‘: $this.getMilliseconds()
      }
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, ($this.getFullYear() + ‘‘).substr(4 - RegExp.$1.length))
      }
      for (var k in o) {
        if (new RegExp(‘(‘ + k + ‘)‘).test(fmt)) {
          fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : ((‘00‘ + o[k]).substr((‘‘ + o[k]).length)))
        }
      }
      return fmt
    },
 
 
调用:
this.formatter(new Date(), ‘yyyy-MM-dd hh:mm:ss‘)
 

原文:https://www.cnblogs.com/majiayin/p/14804058.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐