死亡是一座永恒的灯塔

0%

微信小程序使用wxs将时间戳转化为几天前

需求描述: 文件创建时间与当前时间小于24小时,显示小时数,大于24小时,显示天数,时间戳转化为几天前,几小时前,几分钟前

代码演示wxs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
var getDateDiff = function (dateTimeStamp) {
var minute = 1000 * 60;
var hour = minute * 60;
var day = hour * 24;
var halfamonth = day * 15;
var month = day * 30;
var now = getDate();//有些特殊 不能使用 new Date()
var diffValue = now - dateTimeStamp;
if(diffValue < 0){return;}
var monthC =diffValue/month;
var weekC =diffValue/(7*day);
var dayC =diffValue/day;
var hourC =diffValue/hour;
var minC =diffValue/minute;
if(monthC>=1){
result="" + parseInt(monthC) + "月前";
}
else if(weekC>=1){
result="" + parseInt(weekC) + "周前";
}
else if(dayC>=1){
result=""+ parseInt(dayC) +"天前";
}
else if(hourC>=1){
result=""+ parseInt(hourC) +"小时前";
}
else if(minC>=1){
result=""+ parseInt(minC) +"分钟前";
}else
result="刚刚";
return result;
};//时间戳转化为几天前,几小时前,几分钟前
坚持技术分享,您的支持将鼓励我继续创作!