当前位置:网站首页>uniapp 处理过去时间对比现在时间的时间差 如刚刚、几分钟前,几小时前,几个月前
uniapp 处理过去时间对比现在时间的时间差 如刚刚、几分钟前,几小时前,几个月前
2022-07-04 09:36:00 【gblfy】
1. 返回的报文
格式化时间:createTime
[{
"id": "62c11d3435b7c4007a8e650e",
"fromUserId": "21100598TZ9XG6RP",
"fromNickname": "小美女",
"fromFace": "http://gblfy.cn:9000/imooc/xh.jpg",
"toUserId": "220620BZ2DH0KP94",
"msgType": 1,
"msgContent": {
"isFriend": true
},
"createTime": "2022-07-03 12:38:11"
}
...其他的数据省略
]
2. 时间格式化方法
App.vue定义全局方法,便于调用,单独抽取出来放到工具类中也可以
methods: {
// 时间格式化时间为:刚刚、多少分钟前、多少天前
// stringTime 2020-09-10 20:20:20
getDateBeforeNow(stringTime) {
console.log("传参未格式化", stringTime);
stringTime = new Date(stringTime.replace(/-/g, '/'))
// 统一单位换算
var minute = 1000 * 60;
var hour = minute * 60;
var day = hour * 24;
var week = day * 7;
var month = day * 30;
var year = month * 12;
var time1 = new Date().getTime(); //当前的时间戳
console.log("当前时间", time1);
// 对时间进行毫秒单位转换
var time2 = new Date(stringTime).getTime(); //指定时间的时间戳
console.log("传过来的时间", time2);
var time = time1 - time2;
console.log("计算后的时间", time);
var result = null;
if (time < 0) {
// alert("设置的时间不能早于当前时间!");
result = stringTime;
} else if (time / year >= 1) {
result = parseInt(time / year) + "年前";
} else if (time / month >= 1) {
result = parseInt(time / month) + "月前";
} else if (time / week >= 1) {
result = parseInt(time / week) + "周前";
} else if (time / day >= 1) {
result = parseInt(time / day) + "天前";
} else if (time / hour >= 1) {
result = parseInt(time / hour) + "小时前";
} else if (time / minute >= 1) {
result = parseInt(time / minute) + "分钟前";
} else {
result = "刚刚";
}
console.log("格式化后的时间", result);
return result;
},
}
3. 使用
页面
<template>
<view class="msg-item-middle">
<text class="user-nickname">{
{
msg.fromNickname}}</text>
<text class="msg-content">关注了你 {
{
getGraceDateBeforeNow(msg.createTime)}}</text>
</view>
</template>
方法区
<script>
var app = getApp();
export default {
methods: {
// 时间显示优化 刚刚、几分钟前,几小时前,几个月前
getGraceDateBeforeNow(dateTimeStr) {
return getApp().getDateBeforeNow(dateTimeStr);
},
}
}
</script>
边栏推荐
- C language pointer interview question - the second bullet
- Write a jison parser from scratch (2/10): learn the correct posture of the parser generator parser generator
- Log cannot be recorded after log4net is deployed to the server
- Hands on deep learning (45) -- bundle search
- Hands on deep learning (38) -- realize RNN from scratch
- MySQL case
- pcl::fromROSMsg报警告Failed to find match for field ‘intensity‘.
- 2022-2028 global probiotics industry research and trend analysis report
- Sort out the power node, Mr. Wang he's SSM integration steps
- Custom type: structure, enumeration, union
猜你喜欢
Hands on deep learning (34) -- sequence model
Leetcode (Sword finger offer) - 35 Replication of complex linked list
SQL replying to comments
Hands on deep learning (37) -- cyclic neural network
2022-2028 global special starch industry research and trend analysis report
直方图均衡化
2022-2028 global elastic strain sensor industry research and trend analysis report
Application of safety monitoring in zhizhilu Denggan reservoir area
2022-2028 global gasket plate heat exchanger industry research and trend analysis report
Hands on deep learning (41) -- Deep recurrent neural network (deep RNN)
随机推荐
Kotlin: collection use
Log cannot be recorded after log4net is deployed to the server
Modules golang
入职中国平安三周年的一些总结
什么是 DevSecOps?2022 年的定义、流程、框架和最佳实践
2022-2028 global probiotics industry research and trend analysis report
2022-2028 global small batch batch batch furnace industry research and trend analysis report
2022-2028 global industry research and trend analysis report on anterior segment and fundus OTC detectors
How can Huawei online match improve the success rate of player matching
MySQL develops small mall management system
Pcl:: fromrosmsg alarm failed to find match for field 'intensity'
百度研发三面惨遭滑铁卢:面试官一套组合拳让我当场懵逼
2022-2028 global elastic strain sensor industry research and trend analysis report
【leetcode】29. Divide two numbers
2022-2028 global seeder industry research and trend analysis report
[on February 11, 2022, the latest and most fully available script library collection of the whole network, a total of 23]
Golang Modules
2022-2028 global strain gauge pressure sensor industry research and trend analysis report
C # use gdi+ to add text with center rotation (arbitrary angle)
Hands on deep learning (38) -- realize RNN from scratch