時間轉化,多少分鐘前,多少秒前

jo_an_na發表於2018-07-04
// 轉化時間ago
    timeago (dateTimeStamp) {
      var result = ''
      var minute = 1000 * 60 // 把分,時,天,周,半個月,一個月用毫秒錶示
      var hour = minute * 60
      var day = hour * 24
      var week = day * 7
      var month = day * 30
      var now = new Date().getTime() // 獲取當前時間毫秒
      var diffValue = now - dateTimeStamp // 時間差
      if (diffValue < 0) {
        return
      }
      var minC = diffValue / minute
      var hourC = diffValue / hour
      var dayC = diffValue / day
      var weekC = diffValue / week
      var monthC = diffValue / month
      var minuteAgo = this.$dirct('分鐘前')
      var hourAgo = this.$dirct('小時前')
      var dayAgo = this.$dirct('天前')
      var weekAgo = this.$dirct('周前')
      var monthAgo = this.$dirct('月前')
      if (minC <= 60) {
        result = ' ' + parseInt(minC) + minuteAgo
      } else if (hourC <= 24) {
        result = ' ' + parseInt(hourC) + hourAgo
      } else if (dayC >= 1) {
        result = ' ' + parseInt(dayC) + dayAgo
      } else if (weekC >= 1) {
        result = ' ' + parseInt(weekC) + weekAgo
      } else if (monthC >= 1) {
        result = ' ' + parseInt(monthC) + monthAgo
      } else {
        result = '剛剛'
      }
      return result
    },

相關文章