// 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 + '分钟前';
}
}
}
}
}
}
},
Android和ios时间兼容性写法(js)
最新推荐文章于 2023-04-27 21:15:47 发布