js- time conversion method

default {Export 
  // formatted date 
  / ** 
   * into the specified format Date String 
   * month (M), day (d), 12 (h) hours, 24 hours (H), minutes (m), second ( s), circumference (E), the quarter (Q), BLD (T) may be 1-2 placeholder 
   * in (y) may be 1-4 placeholder milliseconds (S) can only be used a placeholder (1-3 digits) 
   * EG: 
   * the format (new new a Date (), "the mM-dd-YYYY HH: mm: ss.s") ==> 2006-07-02 08: 09: 04.423 
   * the format (new new a Date (), "the mM-YYYY-E dd HH: mm: SS") ==> two 2009-03-10 20:09:04 
   * the format (new new a Date (), "YYYY- mM-dd EE hh: mm: ss ") ==> 2009-03-10 Tuesday 08:09:04 
   * the format (new new a Date ()," the mM-YYYY the EEE-dd HH: mm: SS ") ==> Tuesday 2009-03-10 08:09:04 
   * the format (new new a Date (), "YYYY Md-H: m: sS") ==> 2006-7-2. 8:. 9: 4.18 
   * the format (new new a Date () , "yyyy / mM / dd ( EEE) T hh: mm") ==> 2017/09/06 ( Wednesday) 05 pm:01
   *
   */
  DateUtil: {
    date (val) {
      if (!val) return false
      if (!val.getTime) {
        if (typeof val === 'number') return new Date(val)
        let arr = val.split(/[- : T /]/)
        arr[1] -= 1
        return new Date(...arr)
      } else {
        return val
      }
    },
    format (date, fmt) {
      date = this.date(date)
      let o = {
        'M+': date.getMonth() + 1, // 月份
        'd+': date.getDate(), // 日
        // 'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12, // 小时
        'h+': date.getHours(), // 小时
        'H+': date.getHours(),// hours
        'm +': date.getMinutes () , // points 
        's +': date.getSeconds () , // s 
        'q +': Math.floor (( date.getMonth () + 3) / 3), // quarter 
        S: date.getMilliseconds () // ms 
      } 
      the let Week = { 
        '0': '\ u65e5', 
        '. 1': '\ u4e00', 
        '2': '\ u4e8c', 
        '. 3': '\ u4e09' , 
        '. 4': '\ u56db', 
        '. 5': '\ u4e94', 
        '. 6': '\ u516d' 
      } 
      the let Time = { 
        '0': '\ u51cc \ u6668', aM // 
        '1': '\ u4e0a \ u5348', // afternoon 
        '2': '\ u4e0b \ u5348 ', // night 
        ' 3 ':' \ u665a \ u4e0a '// am 
      } 
      IF (/(y+)/.test(fmt)) { 
        FMT = fmt.replace (
          RegExp.$1,
          (date.getFullYear() + '').substr(4 - RegExp.$1.length)
        )
      }
      if (/(E+)/.test(fmt)) {
        fmt = fmt.replace(
          RegExp.$1,
          (RegExp.$1.length > 1
            ? RegExp.$1.length > 2
              ? '/u661f/u671f'
              : '/u5468'
            : '') + week[date.getDay() + '']
        )
      }
      if (/(T)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, time[parseInt(date.getHours() / 6)])
      }
      for (let 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 unescape(fmt.replace(/\/u/g, '%u'))
    }
  }
}

  

Guess you like

Origin www.cnblogs.com/fm060/p/11106340.html
Recommended