【参考】:https://www.cnblogs.com/zjjDaily/p/8963327.html
问题 处理后的代码 image.png image.png 只有 null 会报错,其他会原样输出function getshowtime(createtime) {
if(createtime){ // 不为 null 时,才去替换
createtime = createtime.replace(/-/g, "/"); // 处理 ios 不兼容问题
}
var creatime = new Date(createtime).getTime()
var nowtime = new Date().getTime()
var calculatetime = nowtime - creatime
var showtime
if (calculatetime < 3600000) {
showtime = Math.floor(calculatetime / (1000 * 60)) + '分钟前'
// showtime = new Date(calculatetime).getMinutes() + '分钟前'
} else if (calculatetime >= 3600000 && calculatetime < 86400000) {
showtime = Math.floor(calculatetime / (1000 * 60 * 60)) + '小时前'
// showtime = new Date(calculatetime).getHours() + '小时前'
} else if (calculatetime >= 86400000 && calculatetime < 172800000) {
showtime = '昨天'
} else if (calculatetime >= 172800000 && calculatetime < 259200000) {
showtime = '前天'
} else if (calculatetime >= 259200000) {
showtime = Math.floor(calculatetime / 86400000) + '天前'
// showtime = new Date(calculatetime).getDay() + '天前'
}
return showtime
}
网友评论