当前位置:网站首页>The time difference between the past time and the present time of uniapp processing, such as just, a few minutes ago, a few hours ago, a few months ago
The time difference between the past time and the present time of uniapp processing, such as just, a few minutes ago, a few hours ago, a few months ago
2022-07-04 10:06:00 【gblfy】
List of articles
1. Message returned
Format time :createTime
[{
"id": "62c11d3435b7c4007a8e650e",
"fromUserId": "21100598TZ9XG6RP",
"fromNickname": " Little beauty ",
"fromFace": "http://gblfy.cn:9000/imooc/xh.jpg",
"toUserId": "220620BZ2DH0KP94",
"msgType": 1,
"msgContent": {
"isFriend": true
},
"createTime": "2022-07-03 12:38:11"
}
... Other data omitted
]
2. Time format method
App.vue Define global methods , Easy to call , It can also be extracted separately and put into the tool class
methods: {
// The time format time is : just 、 How many minutes ago 、 How many days ago
// stringTime 2020-09-10 20:20:20
getDateBeforeNow(stringTime) {
console.log(" The parameter is not formatted ", stringTime);
stringTime = new Date(stringTime.replace(/-/g, '/'))
// Unified unit conversion
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(); // The current timestamp
console.log(" current time ", time1);
// Millisecond unit conversion of time
var time2 = new Date(stringTime).getTime(); // Timestamp of the specified time
console.log(" The time of transmission ", time2);
var time = time1 - time2;
console.log(" Time after calculation ", time);
var result = null;
if (time < 0) {
// alert(" The set time cannot be earlier than the current time !");
result = stringTime;
} else if (time / year >= 1) {
result = parseInt(time / year) + " Years ago ";
} else if (time / month >= 1) {
result = parseInt(time / month) + " Month ago, ";
} else if (time / week >= 1) {
result = parseInt(time / week) + " Zhou Qian ";
} else if (time / day >= 1) {
result = parseInt(time / day) + " Days ago, ";
} else if (time / hour >= 1) {
result = parseInt(time / hour) + " Hours before ";
} else if (time / minute >= 1) {
result = parseInt(time / minute) + " Minutes ago ";
} else {
result = " just ";
}
console.log(" Formatted time ", result);
return result;
},
}
3. Use
page
<template>
<view class="msg-item-middle">
<text class="user-nickname">{
{
msg.fromNickname}}</text>
<text class="msg-content"> Pay attention to you {
{
getGraceDateBeforeNow(msg.createTime)}}</text>
</view>
</template>
Method area
<script>
var app = getApp();
export default {
methods: {
// Time display optimization just 、 A few minutes ago , A few hours ago , A few months ago
getGraceDateBeforeNow(dateTimeStr) {
return getApp().getDateBeforeNow(dateTimeStr);
},
}
}
</script>
边栏推荐
- 用数据告诉你高考最难的省份是哪里!
- MongoDB数据日期显示相差8小时 原因和解决方案
- libmysqlclient. so. 20: cannot open shared object file: No such file or directory
- Hands on deep learning (43) -- machine translation and its data construction
- Golang Modules
- 【Day2】 convolutional-neural-networks
- Batch distribution of SSH keys and batch execution of ansible
- Deep learning 500 questions
- Exercise 9-1 time conversion (15 points)
- xxl-job惊艳的设计,怎能叫人不爱
猜你喜欢
Pcl:: fromrosmsg alarm failed to find match for field 'intensity'
Summary of small program performance optimization practice
Hands on deep learning (41) -- Deep recurrent neural network (deep RNN)
Nuxt reports an error: render function or template not defined in component: anonymous
【Day1】 deep-learning-basics
Hands on deep learning (35) -- text preprocessing (NLP)
Hands on deep learning (36) -- language model and data set
回复评论的sql
libmysqlclient.so.20: cannot open shared object file: No such file or directory
libmysqlclient. so. 20: cannot open shared object file: No such file or directory
随机推荐
How can Huawei online match improve the success rate of player matching
C # use ffmpeg for audio transcoding
Kotlin set operation summary
Ruby时间格式转换strftime毫秒匹配格式
Press the button wizard to learn how to fight monsters - identify the map, run the map, enter the gang and identify NPC
Log cannot be recorded after log4net is deployed to the server
pcl::fromROSMsg报警告Failed to find match for field ‘intensity‘.
Exercise 8-10 output student grades (20 points)
【Day2】 convolutional-neural-networks
基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 2
A little feeling
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
2021-08-10 character pointer
Kubernetes CNI 插件之Fabric
Laravel文档阅读笔记-How to use @auth and @guest directives in Laravel
Hands on deep learning (III) -- Torch Operation (sorting out documents in detail)
Some summaries of the third anniversary of joining Ping An in China
Check 15 developer tools of Alibaba
Pcl:: fromrosmsg alarm failed to find match for field 'intensity'
原生div具有编辑能力