您的位置:首页 > 其它

计算目标时间与当前时间的相差时

2018-03-09 16:55 176 查看
接收一个时间参数,返回传入的目标时间与现在时间相差多久

function timerFormatter(date) {
var now = new Date().getTime();
var timer = now - new Date(date).getTime();

if(timer/1000 <= 10) {
return '刚刚';
}else if(timer/1000 <= 60) {
return Math.floor(timer/1000/60) + '秒前';
}else if(timer/1000/60 <= 60) {
return Math.floor(timer/1000/60) + '分前';
}else if(timer/1000/60/60 <= 24) {
return Math.floor(timer/1000/60/60) + '小时前';
}else if(timer/1000/60/60/24 <= 7) {
return Math.floor(timer/1000/60/60/24) + '天前';
}else if(timer/1000/60/60/24/7 <= 5) {
return Math.floor(timer/1000/60/60/24/7) + '周前';
}else if(timer/1000/60/60/24/31 <= 12) {
return Math.flo
942c
or(timer/1000/60/60/24/31) + '月前';
}else {
return Math.floor(timer/1000/60/60/24/365) + '年前';
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐