创建项目

This commit is contained in:
Putoo
2026-04-21 16:54:18 +08:00
commit 6cdceccde7
59 changed files with 15320 additions and 0 deletions

View File

@@ -0,0 +1,181 @@
/**
* 日期处理工具类(静态工具)
* 提供日期格式化、日期差计算等功能
*/
export class DateEXTEND {
/**
* 格式化时间戳为本地时间字符串
* @param timestamp 时间戳(毫秒)
* @returns 格式化后的时间字符串2026-04-09 23:59:59
*/
static format(timestamp: number | Date): string {
if (!timestamp) return ''
const date = timestamp instanceof Date ? timestamp : new Date(timestamp)
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
const hour = String(date.getHours()).padStart(2, '0')
const minute = String(date.getMinutes()).padStart(2, '0')
const second = String(date.getSeconds()).padStart(2, '0')
return `${year}-${month}-${day} ${hour}:${minute}:${second}`
}
/**
* 格式化日期(仅日期部分)
* @param timestamp 时间戳(毫秒)
* @returns 格式化后的日期字符串2026-04-09
*/
static formatDate(timestamp: number | Date): string {
if (!timestamp) return ''
const date = timestamp instanceof Date ? timestamp : new Date(timestamp)
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
return `${year}-${month}-${day}`
}
/**
* 格式化时间(仅时间部分)
* @param timestamp 时间戳(毫秒)
* @returns 格式化后的时间字符串23:59:59
*/
static formatTime(timestamp: number | Date): string {
if (!timestamp) return ''
const date = timestamp instanceof Date ? timestamp : new Date(timestamp)
const hour = String(date.getHours()).padStart(2, '0')
const minute = String(date.getMinutes()).padStart(2, '0')
const second = String(date.getSeconds()).padStart(2, '0')
return `${hour}:${minute}:${second}`
}
/**
* 计算两个时间戳的天数差
* @param start 开始时间戳
* @param end 结束时间戳
* @returns 天数差正数end在start之后负数end在start之前
*/
static diffDay(start: number, end: number): number {
const oneDay = 1000 * 60 * 60 * 24
return Math.floor((end - start) / oneDay)
}
/**
* 计算两个时间戳的小时差
* @param start 开始时间戳
* @param end 结束时间戳
* @returns 小时差
*/
static diffHour(start: number, end: number): number {
const oneHour = 1000 * 60 * 60
return Math.floor((end - start) / oneHour)
}
/**
* 计算两个时间戳的分钟差
* @param start 开始时间戳
* @param end 结束时间戳
* @returns 分钟差
*/
static diffMinute(start: number, end: number): number {
const oneMinute = 1000 * 60
return Math.floor((end - start) / oneMinute)
}
/**
* 判断是否为今天
* @param timestamp 时间戳(毫秒)
* @returns boolean 是今天返回true否则返回false
*/
static isToday(timestamp: number | Date): boolean {
const today = new Date()
const target = timestamp instanceof Date ? timestamp : new Date(timestamp)
return (
today.getFullYear() === target.getFullYear() &&
today.getMonth() === target.getMonth() &&
today.getDate() === target.getDate()
)
}
/**
* 判断是否为昨天
* @param timestamp 时间戳(毫秒)
* @returns boolean 是昨天返回true否则返回false
*/
static isYesterday(timestamp: number | Date): boolean {
const yesterday = new Date()
yesterday.setDate(yesterday.getDate() - 1)
const target = timestamp instanceof Date ? timestamp : new Date(timestamp)
return (
yesterday.getFullYear() === target.getFullYear() &&
yesterday.getMonth() === target.getMonth() &&
yesterday.getDate() === target.getDate()
)
}
/**
* 智能时间显示(聊天场景使用)
* @param timestamp 时间戳(毫秒)
* @returns 智能时间字符串
*/
static smartFormat(timestamp: number | Date): string {
const now = Date.now()
const target = timestamp instanceof Date ? timestamp.getTime() : timestamp
const diff = now - target
// 小于1分钟
if (diff < 60 * 1000) {
return '刚刚'
}
// 小于1小时
if (diff < 60 * 60 * 1000) {
return `${Math.floor(diff / (60 * 1000))}分钟前`
}
// 小于24小时
if (diff < 24 * 60 * 60 * 1000) {
return `${Math.floor(diff / (60 * 60 * 1000))}小时前`
}
// 今天是显示时间
if (this.isToday(target)) {
return this.formatTime(target)
}
// 昨天显示昨天 + 时间
if (this.isYesterday(target)) {
return `昨天 ${this.formatTime(target)}`
}
// 其他显示日期时间
return this.format(target)
}
/**
* 获取当前时间戳(毫秒)
* @returns 当前时间戳
*/
static now(): number {
return Date.now()
}
/**
* 获取今天开始的时间戳
* @returns 今天开始的时间戳
*/
static getTodayStart(): number {
const today = new Date()
today.setHours(0, 0, 0, 0)
return today.getTime()
}
/**
* 获取今天结束的时间戳
* @returns 今天结束的时间戳
*/
static getTodayEnd(): number {
const today = new Date()
today.setHours(23, 59, 59, 999)
return today.getTime()
}
}