格式化日期

315 阅读1分钟
方法一:返回数据的时候改掉

 // 日期转化
    timeToNormal(dateArr) {
      // 日期转化
      dateArr.forEach(item => {
        const year = new Date(item.addDate).getFullYear()
        const month =
          new Date(item.addDate).getMonth() + 1 < 10
            ? `0${new Date(item.addDate).getMonth() + 1}`
            : new Date(item.addDate).getMonth() + 1
        const date =
          new Date(item.addDate).getDate() < 10
            ? `0${new Date(item.addDate).getDate()}`
            : new Date(item.addDate).getDate()
        const hours =
          new Date(item.addDate).getHours() < 10
            ? `0${new Date(item.addDate).getHours()}`
            : new Date(item.addDate).getHours()
        const minutes =
          new Date(item.addDate).getMinutes() < 10
            ? `0${new Date(item.addDate).getMinutes()}`
            : new Date(item.addDate).getMinutes()
        const seconds =
          new Date(item.addDate).getSeconds() < 10
            ? `0${new Date(item.addDate).getSeconds()}`
            : new Date(item.addDate).getSeconds()
        item.addDate = `${year}-${month}-${date} ${hours}:${minutes}:${seconds}`
      })
    },
方法二:渲染的时候
// 格式化时间
    FormatTime(time) {
      const timer = new Date(time)
      const year = timer.getFullYear()
      const mouth = timer.getMonth() + 1 > 10 ? timer.getMonth() + 1 : '0' + (timer.getMonth() + 1)
      const date = timer.getDate() > 10 ? timer.getDate() : '0' + timer.getDate()
      const hours = timer.getHours() > 10 ? timer.getHours() : '0' + timer.getHours()
      const min = timer.getMinutes() > 10 ? timer.getMinutes() : '0' + timer.getMinutes()
      const secon = timer.getSeconds() > 10 ? timer.getSeconds() : '0' + timer.getSeconds()
      return year + '-' + mouth + '-' + date + ' ' + hours + ':' + min + ':' + secon
    }