Zlpt_Portal/src/utils/time.ts

141 lines
4.2 KiB
TypeScript
Raw Normal View History

2023-11-30 10:49:45 +08:00
import moment from 'moment'
/* eslint-disable @typescript-eslint/no-explicit-any */
2023-12-10 02:44:01 +08:00
/*
2023-11-30 10:49:45 +08:00
https://blog.csdn.net/qq_36228377/article/details/125196997
*/
export function formatTime(curTime: string, formatVal: string) {
2023-12-10 02:44:01 +08:00
/*
2023-11-30 10:49:45 +08:00
curTIme:要格式化的时间
2023-12-10 02:44:01 +08:00
formatVal:格式化时间的格式
2023-11-30 10:49:45 +08:00
*/
// 例如 curTime 2021-02-03 12-21-22 formatVal YYYY年MM月DD日 HH时mm分ss秒
return moment(curTime).format(formatVal)
}
// 时间格式
/* M 112
MM 0112
MMM Jan到Dec
MMMM January到December
Q 14
D 131
DD 0131
d 0606
ddd Sun到Sat
dddd Sunday到Saturday
w 4242
YYYY 2014 2000
YY 14 98
A AM PM AM PM
a am pm am pm
HH 24 0023
H 24 023
hh 12 0012
h 12 012
m 059
mm 0059
s 159
ss 0159
X Unix时间戳 1411572969 */
export function getDiffTime(start: string, end: string, unit: any) {
2023-12-10 02:44:01 +08:00
/*
2023-11-30 10:49:45 +08:00
start
end
unit
*/
moment(start).diff(moment(end), unit) //32
}
/**
*
* timestamp
*/
export function formatDate(timestamp: any) {
// 补全为13位
const arrTimestamp: any = (timestamp + '').split('')
for (let start = 0; start < 13; start++) {
if (!arrTimestamp[start]) {
arrTimestamp[start] = '0'
}
}
timestamp = arrTimestamp.join('') * 1
const minute = 1000 * 60
const hour = minute * 60
const day = hour * 24
const month = day * 30
const now = new Date().getTime()
const diffValue = now - timestamp
// 如果本地时间反而小于变量时间
if (diffValue < 0) {
return '不久前'
}
// 计算差异时间的量级
const monthC: any = diffValue / month
const weekC: any = diffValue / (7 * day)
const dayC: any = diffValue / day
const hourC: any = diffValue / hour
const minC: any = diffValue / minute
// 数值补0方法
const zero = function (value: number) {
if (value < 10) {
return '0' + value
}
return value
}
// 使用
if (monthC > 4) {
// 超过1年直接显示年月日
return (function () {
const date = new Date(timestamp)
return (
date.getFullYear() +
'年' +
zero(date.getMonth() + 1) +
'月' +
zero(date.getDate()) +
'日'
)
})()
} else if (monthC >= 1) {
return parseInt(monthC) + '月前'
} else if (weekC >= 1) {
return parseInt(weekC) + '周前'
} else if (dayC >= 1) {
return parseInt(dayC) + '天前'
} else if (hourC >= 1) {
return parseInt(hourC) + '小时前'
} else if (minC >= 1) {
return parseInt(minC) + '分钟前'
}
return '刚刚'
}
2023-12-08 17:31:40 +08:00
2023-12-10 02:44:01 +08:00
export const getNewDay = (dateTemp: any, days: any,separator:any = '.') => {
2023-12-08 17:31:40 +08:00
console.log("dateTemp",dateTemp,days)
if(!dateTemp ){
return ""
}
2023-12-10 02:44:01 +08:00
dateTemp = dateTemp.split(separator);
2023-12-08 17:31:40 +08:00
//转换为MM-DD-YYYY格式
let nDate: any = new Date(dateTemp[1] + "-" + dateTemp[2] + "-" + dateTemp[0]);
let millSeconds: any = Math.abs(nDate) + days * 24 * 60 * 60 * 1000;
let rDate: any = new Date(millSeconds);
let year: any = rDate.getFullYear();
let month: any = rDate.getMonth() + 1;
if (month < 10) month = "0" + month;
let date = rDate.getDate();
if (date < 10) date = "0" + date;
return year + "." + month + "." + date;
2023-12-10 02:44:01 +08:00
}