当前位置:网站首页>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>
边栏推荐
- Regular expression (I)
- Write a jison parser from scratch (2/10): learn the correct posture of the parser generator parser generator
- 2. Data type
- 7-17 crawling worms (15 points)
- Kotlin set operation summary
- Kotlin 集合操作汇总
- 百度研发三面惨遭滑铁卢:面试官一套组合拳让我当场懵逼
- Hands on deep learning (34) -- sequence model
- What is devsecops? Definitions, processes, frameworks and best practices for 2022
- Advanced technology management - how to design and follow up the performance of students at different levels
猜你喜欢

用数据告诉你高考最难的省份是哪里!

Baidu R & D suffered Waterloo on three sides: I was stunned by the interviewer's set of combination punches on the spot

A little feeling

华为联机对战如何提升玩家匹配成功几率

el-table单选并隐藏全选框

Hands on deep learning (42) -- bi-directional recurrent neural network (BI RNN)

【Day1】 deep-learning-basics

How can people not love the amazing design of XXL job

MongoDB数据日期显示相差8小时 原因和解决方案

Normal vector point cloud rotation
随机推荐
AUTOSAR从入门到精通100讲(106)-域控制器中的SOA
Hands on deep learning (32) -- fully connected convolutional neural network FCN
基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 2
Hands on deep learning (38) -- realize RNN from scratch
System. Currenttimemillis() and system Nanotime (), which is faster? Don't use it wrong!
C # use smtpclient The sendasync method fails to send mail, and always returns canceled
[200 opencv routines] 218 Multi line italic text watermark
Application of safety monitoring in zhizhilu Denggan reservoir area
xxl-job惊艳的设计,怎能叫人不爱
Exercise 7-2 finding the maximum value and its subscript (20 points)
2021-08-10 character pointer
Golang defer
Press the button wizard to learn how to fight monsters - identify the map, run the map, enter the gang and identify NPC
入职中国平安三周年的一些总结
Custom type: structure, enumeration, union
直方图均衡化
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
MySQL case
原生div具有编辑能力
Exercise 9-4 finding books (20 points)