时间差显示XX分钟前

export function timeTo(timestamp) {
  let newDate = Date.parse(new Date());
  let date = new Date(timestamp);
  let dateDiff = newDate - date;
  if (dateDiff < 3600000) {
    return Math.round(dateDiff / 1000 / 60) === 0 ? '刚刚' : Math.round(dateDiff / 1000 / 60) + '分钟前';
  }
  let h = date.getHours() > 10 ? date.getHours() + ':' : '0' + date.getHours() + ':';
  let m = date.getMinutes() > 10 ? date.getMinutes() : '0' + date.getMinutes();
  let mixData = h + m;
  return h + m;
}

猜你喜欢

转载自blog.csdn.net/bangbDIV/article/details/81381699