js时间戳按照指定规则显示

要求是:
不足一分钟:刚刚,
满一分钟不足24小时:xx分钟前
过了0点:昨天,2天前、3天前
超过三天:08-01(月-日)

谁可以帮我改一下吗?
这是我以前用的方法:

getDateDiff(dateTimeStamp) {   //dateTimeStamp是一个时间毫秒,注意时间戳是秒的形式,在这个毫秒的基础上除以1000,就是十位数的时间戳。13位数的都是时间毫秒。
      var result = ''
      var minute = 1000 * 60
      var hour = minute * 60
      var day = hour * 24
      var month = day * 30
      var now = new Date().getTime()
      var diffValue = now - dateTimeStamp
      if (diffValue < 0) return
      var monthC = diffValue / month
      var weekC = diffValue / (7 * day)
      var dayC = diffValue / day
      var hourC = diffValue / hour
      var minC = diffValue / minute
      if (monthC >= 1) {
        result = "" + parseInt(monthC) + "月前"
      }
      else if (weekC>=1) {
        result = "" + parseInt(weekC) + "周前"
      }
      else if (dayC >= 1) {
        result = ""+ parseInt(dayC) + "天前"
      }
      else if (hourC >= 1){
        result = "" + parseInt(hourC) + "小时前"
      }
      else if (minC >= 1) {
        result = ""+ parseInt(minC) + "分钟前"
      } else {
        result = "刚刚"
      }
      return result
    }
阅读 1.9k
1 个回答
function getDateDiff(before, after=Date.now()) {
  
  function getDate(time) {
    return new Date(typeof time == 'string' ? time.replace(/-/g,'/') : time);
  }
  
  function padStart(src,len,pad) {
    var target = src.toString()
    while(target.length < len) {
      target = pad + target;
    }
    return target;
  }
  
  var prev = getDate(before)
  var now = getDate(after)
  var dvalue = (now-prev)/1000|0;
  var minute = 60
  var day = 60*60*24
  // 不足24小时
  if(dvalue<day) {
    // 不在同一天显示昨天
    if(now.getDate() == prev.getDate()) {
      return dvalue < minute ? '刚刚' : ((dvalue/minute|0)+'分钟前')
    }
    return '昨天';
  }
     
  var dayDvalue = now.getDate()-prev.getDate();
  if(dvalue < 4*day && dayDvalue<4) return dayDvalue == 1 ? '昨天' : dayDvalue + '天前';
  
  return padStart(prev.getMonth()+1, 2, '0') + '-' + padStart(prev.getDate(),2,'0');
}

function test(time, except, msg) {
  console.assert( getDateDiff(time, '2020-9-8 12:00:00') === except, msg );
}

test( '2020-9-8 11:59:59', '刚刚', '1分钟内->刚刚[2020-9-8 11:59:59]' )

test( '2020-9-8 11:59:01', '刚刚', '1分钟内->刚刚[2020-9-8 11:59:01]' )

test( '2020-9-8 11:59:00', '1分钟前', '超过1分钟不足24小时->x分钟前[2020-9-8 11:59:00]' )

test( '2020-9-8 11:58:01', '1分钟前', '超过1分钟不足24小时->x分钟前[2020-9-8 11:58:00]' )

test( '2020-9-8 00:00:01', 12*60-1+'分钟前', '超过1分钟不足24小时->x分钟前[2020-9-8 00:00:01]' )

test( '2020-9-7 23:59:59', '昨天', '过0点->昨天[2020-9-7 23:59:59]' )

test( '2020-9-7 00:00:01', '昨天', '过0点->昨天[2020-9-7 00:00:01]' )

test( '2020-9-6 23:59:59', '2天前', '过0点非昨天->x天前[2020-9-6 23:59:59]' )

test( '2020-9-6 00:00:01', '2天前', '过0点非昨天->x天前[2020-9-6 00:00:01]' )

test( '2020-9-5 23:59:59', '3天前', '过0点非昨天->x天前[2020-9-5 23:59:59]' )

test( '2020-9-5 00:00:01', '3天前', '过0点非昨天->x天前[2020-9-5 00:00:01]' )

test( '2020-9-4 23:59:59', '09-04', '超过3天->月-日[2020-9-4 23:59:59]' )

test( '2020-9-4 00:00:01', '09-04', '超过3天->月-日[2020-9-4 00:00:01]' )
推荐问题
宣传栏