当前位置:网站首页>util. Collection and encapsulation of JS tool functions
util. Collection and encapsulation of JS tool functions
2022-06-25 23:40:00 【BetterGG】
function friendlyDate(timestamp) {
var formats = {
'year': '%n% Years ago ',
'month': '%n% Month ago, ',
'day': '%n% Days ago, ',
'hour': '%n% Hours before ',
'minute': '%n% Minutes ago ',
'second': '%n% Seconds ago ',
};
var now = Date.now();
var seconds = Math.floor((now - timestamp) / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);
var months = Math.floor(days / 30);
var years = Math.floor(months / 12);
var diffType = '';
var diffValue = 0;
if (years > 0) {
diffType = 'year';
diffValue = years;
} else {
if (months > 0) {
diffType = 'month';
diffValue = months;
} else {
if (days > 0) {
diffType = 'day';
diffValue = days;
} else {
if (hours > 0) {
diffType = 'hour';
diffValue = hours;
} else {
if (minutes > 0) {
diffType = 'minute';
diffValue = minutes;
} else {
diffType = 'second';
diffValue = seconds === 0 ? (seconds = 1) : seconds;
}
}
}
}
}
return formats[diffType].replace('%n%', diffValue);
}
function getDate() {
let myDate = new Date()
let month = myDate.getMonth()
if (month < 10) {
month = '0' + (month + 1)
}
let day = myDate.getDate()
if (day < 10) {
day = '0' + day
}
let date = myDate.getFullYear() + '-' + month + '-' + day
return date
}
function getTime() {
let myDate = new Date()
let hour = myDate.getHours()
if (hour < 10) {
hour = '0' + hour
}
let min = myDate.getMinutes()
if (min < 10) {
min = '0' + min
}
let time = hour + ':' + min
return time
}
module.exports = {
friendlyDate: friendlyDate,
getDate: getDate,
getTime: getTime
}
main.js Is mounted as a global method in
import { friendlyDate } from './common/util.js'
Vue.prototype.$friendlyDate = friendlyDate
边栏推荐
- Repoptimizer: it's actually repvgg2
- Download the latest V80 version of Google Chrome
- jdbc常见异常及错误解决办法汇总
- 库项目和App项目中清单文件的包名不要相同
- OBS-Studio-27.2.4-Full-Installer-x64.exe 下载
- [opencv450 samples] read the image path list and maintain the proportional display
- [opencv450 samples] create image list yaml
- ACM. Hj16 shopping list ●●
- Graduation trip | recommended 5-day trip to London
- hiberate架构介绍及环境搭建(非常详细)
猜你喜欢

Use of xinchida ble 5.0 Low Power Bluetooth module (at command serial port transparent transmission) rsbrs02abr

做接口测试,这3种工具到底什么时候用?

Repoptimizer: it's actually repvgg2

提取系统apk

1.8 billion pixel Mars panorama Ultra HD released by NASA, very shocking

二进制、16进制、大端小端

28 rounds of interviews with 10 companies in two and a half years (including byte, pinduoduo, meituan, Didi...)

Rk3568+ Hongmeng industrial control board industrial gateway video gateway solution

character string

QComboBox下拉菜单中有分隔符Separator时的样式设置
随机推荐
UE4 学习记录二 给角色添加骨架,皮肤,及运动动画
解决‘tuple‘ object has no attribute ‘lower‘
1.8 billion pixel Mars panorama Ultra HD released by NASA, very shocking
QT custom implemented calendar control
【无标题】打开一个项目连接,无法正常显示时,ping一下ip
Visual studio code create minimal web API (asp.net core)
Share a downloaded osgeo4w64 Library Based on qgis3.10
My vscode
excel如何实现中文单词自动翻译成英文?这个公式教你了
CSDN原力值
Pointer strengthening and improvement
28 rounds of interviews with 10 companies in two and a half years (including byte, pinduoduo, meituan, Didi...)
树状类查询组件
二进制、16进制、大端小端
Qt Utf8 与 Unicode 编码的互相转换, Unicode编码输出为格式为 &#xXXXX
自定义QComboBox下拉框,右对齐显示,下拉列表滑动操作
ACM. HJ16 购物单 ●●
C1. k-LCM (easy version)-Codeforces Round #708 (Div. 2)
RK3568+鸿蒙工控板工业网关视频网关解决方案
C. Yet Another Card Deck-Educational Codeforces Round 107 (Rated for Div. 2)