js根据时间字符串修改为“刚刚”“一分钟前”“一小时前”

【参考】:https://blog.csdn.net/bigbear00007/article/details/80328607?utm_source=app

【代码】:

function getshowtime(createtime) {
  if(createtime){ 
      createtime = createtime.replace(/-/g, "/"); // 处理 ios 不兼容问题
  }
  let createTime = new Date(createtime);
  let createTimes = createTime.getTime();
  let nowTime = new Date();
  let nowTimes =nowTime.getTime();
  if(nowTimes < createTimes){
      return `现在时间之后`
  }

  let createYear = createTime.getFullYear();
  let nowYear = nowTime.getFullYear();
  let createMonth = createTime.getMonth();
  let nowMonth = nowTime.getMonth();
  let createDate = createTime.getDate();
  let nowDate = nowTime.getDate();

  if(createYear=1){
                  return `${parseInt(diffValue/(1000*60*60))}小时前`
              } else {
                  if(diffValue/(1000*60)>=1){
                      return `${parseInt(diffValue/(1000*60))}分钟前`
                  } else {
                      return `刚刚`
                  }
              }
          }
      }
  }
}

module.exports = {
  getshowtime
}

你可能感兴趣的:(js根据时间字符串修改为“刚刚”“一分钟前”“一小时前”)