196 lines
6.0 KiB
TypeScript
Raw Normal View History

2025-07-15 16:18:34 +08:00
import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime'
2025-10-29 13:53:05 +08:00
import 'dayjs/locale/zh-cn'
2025-07-15 16:18:34 +08:00
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]
}
}
2025-10-29 13:53:05 +08:00
export function getTheFutureDay(num = 1) {
//获取未来num天的日期
const pastFifteenDays = dayjs().add(num, 'day')
return pastFifteenDays.format('YYYY-MM-DD')
}
2025-07-15 16:18:34 +08:00
export function getThreeMonthsLater(num = 3, dateString: string) {
const date = dayjs(dateString)
const threeMonthsLater = date.add(num || 3, 'month')
return threeMonthsLater.format('YYYY-MM-DD')
}
2025-10-29 13:53:05 +08:00
export function getCurrentHour() {
const now = new Date()
const currentHour = now.getHours()
const currentMinute = now.getMinutes()
console.log(currentHour, currentMinute, 'dayjs().hour()')
return { currentHour, currentMinute }
// return dayjs().hour()
}
export function generateTimeSlots(externalDate = null) {
// 如果没有外部传入日期,使用当前日期
const baseDate = externalDate ? dayjs(externalDate) : dayjs()
const now = externalDate ? dayjs(externalDate) : dayjs() // 使用相同的日期作为当前时间
const startHour = 8 // 开始时间 8:00
const endHour = 18 // 结束时间 18:00
const interval = 2 // 间隔2小时
const currentTime = now.valueOf() // 当前时间的时间戳
const slots = []
let currentStart = startHour
// 生成所有时间段
while (currentStart < endHour) {
const currentEnd = Math.min(currentStart + interval, endHour)
// 使用基准日期设置具体时间
const startTime = baseDate.hour(currentStart).minute(0).second(0)
const endTime = baseDate.hour(currentEnd).minute(0).second(0)
const startStr = startTime.format('HH:mm')
const endStr = endTime.format('HH:mm')
// 检查当前时间是否在这个时间段内
const isCurrentPeriod = currentTime >= startTime.valueOf() && currentTime < endTime.valueOf()
// 只有当时间段还未结束时才显示
if (endTime.valueOf() > currentTime) {
slots.push({
label: isCurrentPeriod ? '两个小时内' : `${startStr}-${endStr}`,
str: startStr,
end: endStr,
isCurrent: isCurrentPeriod,
date: baseDate.format('YYYY-MM-DD'),
startTimestamp: startTime.valueOf(),
endTimestamp: endTime.valueOf()
})
}
currentStart = currentEnd
}
console.log('slots', slots)
return slots
}
2025-07-15 16:18:34 +08:00
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
2025-10-29 13:53:05 +08:00
export function getTimeRangeDisplay(startTime: string, endTime: string, currentTime = null) {
try {
const start = dayjs(startTime)
const end = dayjs(endTime)
const now = currentTime ? dayjs(currentTime) : dayjs()
if (!start.isValid() || !end.isValid()) {
2026-04-10 15:21:02 +08:00
return ''
2025-10-29 13:53:05 +08:00
}
if (start.isAfter(end)) {
return '时间顺序错误'
}
// 检查当前时间是否在时间段内
if (now.isAfter(start) && now.isBefore(end)) {
return '两个小时内'
}
2025-08-29 09:51:02 +08:00
2025-10-29 13:53:05 +08:00
// 不在时间段内,显示具体时间
const isSameDay = start.isSame(end, 'day')
if (isSameDay) {
return `${start.format('YYYY-MM-DD')} ${start.format('HH:mm')}-${end.format('HH:mm')}`
} else {
return `${start.format('YYYY-MM-DD HH:mm')} - ${end.format('YYYY-MM-DD HH:mm')}`
}
} catch (error) {
return '时间格式错误'
}
}
export function currentYear() {
2025-08-29 09:51:02 +08:00
//获取当前年份
2025-10-29 13:53:05 +08:00
const currentYear = new Date().getFullYear()
console.log('当前年份:', currentYear)
return currentYear + ''
}
export function getFromNow(time: string) {
//获取时间距离现在的时间
return time ? dayjs(time).fromNow() : '' // 22 年前
}
export function getIsInRangeInclusive(start?: string, end?: string) {
const now = dayjs()
// 创建日期段的起始和结束时间的dayjs对象
const startDate = dayjs(start)
const endDate = dayjs(end)
return now.isAfter(startDate) && now.isBefore(endDate)
}
export function getDate(time: string) {
//时间转换为YYYY-MM-DD格式
return time ? dayjs(time).format('YYYY-MM-DD') : ''
}
export function specificTime(targetTime: string) {
const now = dayjs()
const target = dayjs(targetTime)
let hoursDiff = Math.abs(target.diff(now, 'hour', true)) <= 2 || target.isBefore(now)
console.log(hoursDiff, 'hoursDiff')
//是否在2小时内的
return hoursDiff
}
2026-01-30 09:54:26 +08:00
export function showCurrentTime() {
2026-04-10 15:21:02 +08:00
const timeString = dayjs().format('HH:mm:ss')
2026-01-30 09:54:26 +08:00
return timeString
}
export function checkWorkStatus() {
const now = dayjs()
const currentHour = now.hour()
// 上班时间: 8点-18点
if (currentHour >= 8 && currentHour < 18) {
return { status: '在线', value: 'Online' }
} else {
return { status: '已休息', value: 'Offline' }
}
}