日期处理,“刚刚”,“2天前”的时间算法

这个逻辑算法是


function convertTime(timeStr) {
  var lastTime;

  if (typeof timeStr == "string") {
  if (!timeStr)
  return "";
  timeStr = timeStr.replace(/-/g, "/");

  lastTime = new Date(timeStr).getTime();
  }
  else if (typeof timeStr == "number")
  lastTime = timeStr;
  else if (typeof timeStr == "object")
  lastTime = Date.parse(timeStr);

  var nowTime = Date.parse(new Date()); //获取当前时间
  var timeLength = parseInt((nowTime - lastTime) / 1000);
  var str = 0;
  if (timeLength <= 60 * 3) {
  str = "刚刚";
  }
  else if (0 < timeLength && timeLength <= 60 * 5) {
  str = "5分钟前";
  }
  else if (0 < timeLength && timeLength <= 60 * 10) {
  str = "10分钟前";
  }
  else if (0 < timeLength && timeLength <= 60 * 15) {
  str = "15分钟前";
  } else if (60 * 15 < timeLength && timeLength <= 60 * 30) {
  str = "半小时前";
  } else if (60 * 30 < timeLength && timeLength <= 60 * 60) {
  str = "1小时前";
  } else if (60 * 60 < timeLength && timeLength <= 60 * 60 * 24) {
  str = parseInt(timeLength / 3600) + "小时前";
  } else if (60 * 60 * 24 < timeLength && timeLength <= 60 * 60 * 24 * 30) {
  str = parseInt(timeLength / 3600 / 24) + "天前";
  } else if (60 * 60 * 24 * 30 < timeLength && timeLength < 60 * 60 * 24 * 30 * 12) {
  str = parseInt(timeLength / 3600 / 24 / 30) + "月前";
  } else if (60 * 60 * 24 * 30 * 12 < timeLength) {
  str = parseInt(timeLength / 3600 / 24 / 30 / 12) + "年前";
  }
  return str;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值