时间形式的转换

关于时间

获取当前时间:

new Date()                           //  Fri Jan 22 2021 14:40:45 GMT+0800 (中国标准时间)

new Date().getFullYear()     //  2021

new Date().getMonth()        //  0        (获取月时记得 +1 )

new Date().getDate()          //  22

new Date().getHours()        //  14


new Date().getMinutes()      //  43

new Date().getSeconds()     //   39

new Date().getMilliseconds()  //  217     (毫秒)

new Date().getDay()              //  5        (星期)    

new Date().getTimezoneOffset()    //  -480        与格林威治时间的时间差  (单位:分钟)

其他时间形式 转换为 时间戳

new Date().getTime()           //  1611297667687

 

  时间戳  转换成 其他时间形式 

   插件   dayjs:  https://dayjs.fenxianglu.cn/

方法:

 dateFilter(time, type) {
      let date = new Date(time * 1000)
      let year = date.getFullYear()
      let month = date.getMonth() + 1
      let day = date.getDate()
      let hours = date.getHours()
      let minutes = date.getMinutes()
      let second = date.getSeconds()
      let format = (value) => {
        return value >= 10 ? value : '0' + value
      }
      let result
      switch (type) {
        case 0: // 01-05
          result = `${format(month)}月${format(day)}日`
          break
        case 1: // 11:12
          result = `${format(hours)}-${format(minutes)}`
          break
        case 2: // 2015-01-05
          result = `${year}-${format(month)}-${format(day)}`
          break
        case 3: // 2015-01-05 11:12
          result = `${year}-${format(month)}-${format(day)}  ${format(
            hours

 )}:${format(minutes)}`
          break
        case 4: // 2015.01.05 11:12
          result = `${year}.${month}.${day}  ${hours}:${minutes}`
          break
        case 5:
          result = `${year}年${format(month)}月${format(day)}日${format(
            hours

 )}时${format(minutes)}分`
        case 6: // 2015-01-05 11:12:06
          result = `${year}-${format(month)}-${format(day)}  ${format(
            hours
          )}:${format(minutes)}:${format(second)}`
          break
        case 7:
          result = `${year}-${format(month)}`
          break
        case 8:
          result = `${year}年${format(month)}月`
          break
        case 9: // 2015-01-05 11:12:06:32
          result = `${year}-${format(month)}-${format(day)}  ${format(
            hours
          )}:${format(minutes)}:${format(second)}`
          break
      }
      return result
    },
原文地址:https://www.cnblogs.com/dandanyajin/p/14313677.html