当前位置:网站首页>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>
边栏推荐
- IIS configure FTP website
- Implementing expired localstorage cache with lazy deletion and scheduled deletion
- Vanishing numbers
- El Table Radio select and hide the select all box
- PHP is used to add, modify and delete movie information, which is divided into foreground management and background management. Foreground users can browse information and post messages, and backgroun
- Golang defer
- How web pages interact with applets
- Exercise 7-8 converting strings to decimal integers (15 points)
- Luogu deep foundation part 1 Introduction to language Chapter 4 loop structure programming (2022.02.14)
- PHP personal album management system source code, realizes album classification and album grouping, as well as album image management. The database adopts Mysql to realize the login and registration f
猜你喜欢
智能网关助力提高工业数据采集和利用
Kubernetes CNI 插件之Fabric
Ultimate bug finding method - two points
2022-2028 global edible probiotic raw material industry research and trend analysis report
Advanced technology management - how to design and follow up the performance of students at different levels
el-table单选并隐藏全选框
2022-2028 global industrial gasket plate heat exchanger industry research and trend analysis report
How can people not love the amazing design of XXL job
法向量点云旋转
C language pointer interview question - the second bullet
随机推荐
Vanishing numbers
Hands on deep learning (37) -- cyclic neural network
Write a jison parser from scratch (5/10): a brief introduction to the working principle of jison parser syntax
MySQL develops small mall management system
2021-08-11 function pointer
pcl::fromROSMsg报警告Failed to find match for field ‘intensity‘.
Intelligent gateway helps improve industrial data acquisition and utilization
Flutter 小技巧之 ListView 和 PageView 的各種花式嵌套
2022-2028 global visual quality analyzer industry research and trend analysis report
Daughter love in lunch box
How web pages interact with applets
Kotlin 集合操作汇总
Summary of small program performance optimization practice
查看CSDN个人资源下载明细
Luogu deep foundation part 1 Introduction to language Chapter 4 loop structure programming (2022.02.14)
Machine learning -- neural network (IV): BP neural network
2022-2028 global seeder industry research and trend analysis report
Web端自动化测试失败原因汇总
2022-2028 global probiotics industry research and trend analysis report
lolcat