# js计算时间为刚刚、几分钟前、几小时前、几天前··_js计算几天前(1)

174 篇文章 0 订阅

#### 最后

96道前端面试题：

var day = hour * 24;
var week = day * 7;
var halfamonth = day * 15;
var month = day * 30;
var now = new Date().getTime();   //获取当前时间毫秒
console.log(now)
var diffValue = now - dateTimeStamp;//时间差

if(diffValue < 0){
return;
}
var minC = diffValue/minute;  //计算时间差的分，时，天，周，月
var hourC = diffValue/hour;
var dayC = diffValue/day;
var weekC = diffValue/week;
var monthC = diffValue/month;
if(monthC >= 1 && monthC <= 3){
result = " " + parseInt(monthC) + “月前”
}else if(weekC >= 1 && weekC <= 3){
result = " " + parseInt(weekC) + “周前”
}else if(dayC >= 1 && dayC <= 6){
result = " " + parseInt(dayC) + “天前”
}else if(hourC >= 1 && hourC <= 23){
result = " " + parseInt(hourC) + “小时前”
}else if(minC >= 1 && minC <= 59){
result =" " + parseInt(minC) + “分钟前”
}else if(diffValue >= 0 && diffValue <= minute){
result = “刚刚”
}else {
var datetime = new Date();
datetime.setTime(dateTimeStamp);
var Nyear = datetime.getFullYear();
var Nmonth = datetime.getMonth() + 1 < 10 ? “0” + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
var Ndate = datetime.getDate() < 10 ? “0” + datetime.getDate() : datetime.getDate();
var Nhour = datetime.getHours() < 10 ? “0” + datetime.getHours() : datetime.getHours();
var Nminute = datetime.getMinutes() < 10 ? “0” + datetime.getMinutes() : datetime.getMinutes();
var Nsecond = datetime.getSeconds() < 10 ? “0” + datetime.getSeconds() : datetime.getSeconds();
result = Nyear + “-” + Nmonth + “-” + Ndate
}
return result;
}



## 在小程序中查看
![](https://img-blog.csdnimg.cn/2042fe23d45a466e8b9b077648ff5a52.jpeg)

### 结尾

**[开源分享：【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】](https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0)**

![前端学习书籍导图-1](https://img-blog.csdnimg.cn/img_convert/697816e27884b98495722d4fa81a2920.webp?x-oss-process=image/format,png)

ms/4304bb5a486d4c3ab8389e65ecb71ac0)**

[外链图片转存中...(img-msYdtep1-1715788566875)]


• 14
点赞
• 23
收藏
觉得还不错? 一键收藏
• 0
评论
11-17 1万+
08-04 1069
07-04 522
11-22 3808
08-23 2082
09-07 3010
11-04 1950

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