当前位置:网站首页>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>
边栏推荐
- Some summaries of the third anniversary of joining Ping An in China
- 用数据告诉你高考最难的省份是哪里!
- Service developers publish services based on EDAs
- Exercise 7-2 finding the maximum value and its subscript (20 points)
- 转载:等比数列的求和公式,及其推导过程
- Web端自动化测试失败原因汇总
- El Table Radio select and hide the select all box
- Get the source code in the mask with the help of shims
- Go context 基本介绍
- 原生div具有编辑能力
猜你喜欢

Mmclassification annotation file generation

技术管理进阶——如何设计并跟进不同层级同学的绩效

Qtreeview+ custom model implementation example

PHP student achievement management system, the database uses mysql, including source code and database SQL files, with the login management function of students and teachers
Summary of reasons for web side automation test failure

xxl-job惊艳的设计,怎能叫人不爱

法向量点云旋转

How web pages interact with applets

libmysqlclient. so. 20: cannot open shared object file: No such file or directory

Hands on deep learning (33) -- style transfer
随机推荐
入职中国平安三周年的一些总结
Kotlin 集合操作汇总
How to teach yourself to learn programming
SSM online examination system source code, database using mysql, online examination system, fully functional, randomly generated question bank, supporting a variety of question types, students, teache
按键精灵打怪学习-识别所在地图、跑图、进入帮派识别NPC
Check 15 developer tools of Alibaba
Hands on deep learning (36) -- language model and data set
有老师知道 继承RichSourceFunction自定义读mysql怎么做增量吗?
Golang defer
Kotlin: collection use
用数据告诉你高考最难的省份是哪里!
Machine learning -- neural network (IV): BP neural network
百度研发三面惨遭滑铁卢:面试官一套组合拳让我当场懵逼
Kotlin set operation summary
Custom type: structure, enumeration, union
Vanishing numbers
Log cannot be recorded after log4net is deployed to the server
Hands on deep learning (39) -- gating cycle unit Gru
How do microservices aggregate API documents? This wave of show~
A little feeling