2025-07-15 16:18:34 +08:00
|
|
|
import dayjs from 'dayjs'
|
|
|
|
|
import relativeTime from 'dayjs/plugin/relativeTime'
|
|
|
|
|
dayjs.locale('zh-cn') // 设置为中文
|
|
|
|
|
// 使用相对时间插件
|
|
|
|
|
dayjs.extend(relativeTime)
|
|
|
|
|
export function RelativeTime(time: number) {
|
|
|
|
|
return dayjs(time * 1000).fromNow()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function showDay(time: number) {
|
|
|
|
|
return dayjs(time).format('YYYY-MM-DD')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getDay(num?: number) {
|
|
|
|
|
if (num) {
|
|
|
|
|
const yesterday = dayjs().subtract(num, 'day')
|
|
|
|
|
console.log(yesterday.format('YYYY-MM-DD'))
|
|
|
|
|
return yesterday.format('YYYY-MM-DD')
|
|
|
|
|
}
|
|
|
|
|
return dayjs().format('YYYY-MM-DD')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getOverDay(num: number, type: string) {
|
|
|
|
|
const currentDate = dayjs()
|
|
|
|
|
const formattedCurrentDate = currentDate.format('YYYY-MM-DD')
|
|
|
|
|
console.log('当前日期:', formattedCurrentDate)
|
|
|
|
|
if (type === 'day') {
|
|
|
|
|
//获取过去num天的日期
|
|
|
|
|
const pastFifteenDays = dayjs().subtract(num, 'day')
|
|
|
|
|
const formattedPastFifteenDays = pastFifteenDays.format('YYYY-MM-DD')
|
|
|
|
|
return [formattedPastFifteenDays, formattedCurrentDate]
|
|
|
|
|
}
|
|
|
|
|
if (type === 'month') {
|
|
|
|
|
// 获取过去num个月的日期
|
|
|
|
|
const pastTwelveMonths = dayjs().subtract(num, 'month')
|
|
|
|
|
const formattedPastTwelveMonths = pastTwelveMonths.format('YYYY-MM-DD')
|
|
|
|
|
return [formattedPastTwelveMonths, formattedCurrentDate]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getThreeMonthsLater(num = 3, dateString: string) {
|
|
|
|
|
const date = dayjs(dateString)
|
|
|
|
|
const threeMonthsLater = date.add(num || 3, 'month')
|
|
|
|
|
return threeMonthsLater.format('YYYY-MM-DD')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isSameDay(day: string) {
|
|
|
|
|
//是否是当天-还款日期
|
|
|
|
|
const date1 = new Date(day)
|
|
|
|
|
const date2 = new Date()
|
|
|
|
|
return (
|
|
|
|
|
date1.getFullYear() === date2.getFullYear() &&
|
|
|
|
|
date1.getMonth() === date2.getMonth() &&
|
|
|
|
|
date1.getDate() === date2.getDate()
|
|
|
|
|
)
|
|
|
|
|
}
|
2025-08-29 09:51:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
export function currentYear(){
|
|
|
|
|
//获取当前年份
|
|
|
|
|
const currentYear = new Date().getFullYear();
|
|
|
|
|
console.log('当前年份:', currentYear);
|
|
|
|
|
return currentYear+'';
|
|
|
|
|
}
|