Android和ios时间兼容性写法(js)

// ios 不识别 时间为1996-2-3  这种格式要转化为1996/2/3

function timerCal(time){
    let oTime = time
    let postTime = new Date(time.replace(/-/g,'/'))
    let curTime = new Date()
    let timeDiff = curTime.getTime() - postTime.getTime()
    let min = 60 * 1000;
    let hour = min * 60;
    let day = hour * 24;
    let week = day * 7;
    let month =  week*4;
    let year = month*12;
    // 计算发布时间距离当前时间的周、天、时、分
    let exceedyear = Math.floor(timeDiff/year);
    let exceedmonth = Math.floor(timeDiff/month);
    let exceedWeek = Math.floor(timeDiff/week);
    let exceedDay = Math.floor(timeDiff/day);
    let exceedHour = Math.floor(timeDiff/hour);
    let exceedMin = Math.floor(timeDiff/min);
    if(exceedyear<100&&exceedyear>0){
        return oTime.substring(5,7) + '月' + oTime.substring(8,10) + '日' + ' ' +oTime.substring(11,16)
    }else{
        if(exceedmonth<12&&exceedmonth>0){
            return oTime.substring(5,7) + '月' + oTime.substring(8,10) + '日' + ' ' +oTime.substring(11,16)
        }else{
            if(exceedWeek<4&&exceedWeek>0){
                return oTime.substring(5,7) + '月' + oTime.substring(8,10) + '日' + ' ' +oTime.substring(11,16)
            }else{
                if(exceedDay < 7 && exceedDay > 0){
                    return oTime.substring(5,7) + '月' + oTime.substring(8,10) + '日' + ' ' +oTime.substring(11,16)
                }else {
                    if (exceedHour < 24 && exceedHour > 0) {
                        return oTime.substring(5,7) + '月' + oTime.substring(8,10) + '日' + ' ' +oTime.substring(11,16)
                    } else {
                        if(exceedMin==0){
                            return '刚刚';
                        }else{
                            return exceedMin + '分钟前';
                        }

                    }
                }
            }
        }
    }
}


   // 或

   timerCal(time){
      let postTime = new Date(time.replace(/-/g,'/'))
      let curTime = new Date()
      let timeDiff = curTime.getTime() - postTime.getTime()
      let min = 60 * 1000;
      let hour = min * 60;
      let day = hour * 24;
      let week = day * 7;
      let month =  week*4;
      let year = month*12;
      // 计算发布时间距离当前时间的周、天、时、分
      let exceedyear = Math.floor(timeDiff/year);
      let exceedmonth = Math.floor(timeDiff/month);
      let exceedWeek = Math.floor(timeDiff/week);
      let exceedDay = Math.floor(timeDiff/day);
      let exceedHour = Math.floor(timeDiff/hour);
      let exceedMin = Math.floor(timeDiff/min);
      if(exceedyear<100&&exceedyear>0){
        return exceedyear + '年前';
      }else{
        if(exceedmonth<12&&exceedmonth>0){
          return exceedmonth + '月前';
        }else{
          if(exceedWeek<4&&exceedWeek>0){
            return exceedWeek + '星期前';
          }else{
            if(exceedDay < 7 && exceedDay > 0){
              return exceedDay + '天前';
            }else {
              if (exceedHour < 24 && exceedHour > 0) {
                return exceedHour + '小时前';
              } else {
                if(exceedMin==0){
                  return '刚刚';
                }else{
                  return exceedMin + '分钟前';
                }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值