haomingming 5ba50e4e61 优化
2023-03-28 20:52:22 +08:00

80 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const formatTime = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}
const formatChatTime = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return `${[month, day].map(formatNumber).join('-')} ${[hour, minute].map(formatNumber).join(':')}`
}
const getDateArr = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return [year, month, day, hour, minute, second]
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : `0${n}`
}
const getTimeAgo = stringTime =>{
var minute = 1000 * 60;
var hour = minute *60;
var day = hour *24;
var week = day * 7;
var month = day * 30;
var time1 = new Date().getTime();//当前的时间戳
var time2 = stringTime*1000;//指定时间的时间戳
var time = time1 - time2;
var result = null;
if(time < 0){
return '刚刚';
}else if(time/month >= 1){
result = formatChatTime(new Date(time2));
}else if(time/week >= 1){
result = formatChatTime(new Date(time2));
}else if(time/day >= 1){
result = formatChatTime(new Date(time2));
}else if(time/hour >= 1){
result = parseInt(time/hour) + "小时前";
}else if(time/minute >= 1){
result = parseInt(time/minute) + "分钟前";
}else {
result = "刚刚";
}
return result;
}
// px 转换为 rpx 传参类型是数字Number
const rpxTopx = px => {
let deviceWidth = wx.getSystemInfoSync().windowWidth; //获取设备屏幕宽度
let rpx = (750 / deviceWidth) * Number(px)
return Math.floor(rpx);
}
module.exports = {
formatTime,
getDateArr,
formatNumber,
getTimeAgo,
rpxTopx
}