123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- /**
- * 日期格式化工具
- * @param {string|Date} date - 日期字符串或Date对象
- * @param {string} format - 输出格式,支持以下占位符:
- * YYYY - 四位年份
- * YY - 两位年份
- * MM - 两位月份
- * DD - 两位日期
- * HH - 24小时制小时
- * mm - 分钟
- * ss - 秒钟
- * SSS - 毫秒
- * @returns {string} 格式化后的日期字符串
- */
- export function formatDate(date, format = 'YYYY-MM-DD HH:mm') {
- // 处理空值
- if (!date) return ''
- // 创建Date对象
- const dateObj = date instanceof Date ? date : new Date(date)
- // 处理无效日期
- if (isNaN(dateObj.getTime())) {
- console.warn('formatDate: Invalid date input, returning empty string')
- return ''
- }
- // 填充数字到指定长度
- const pad = (num, length = 2) => String(num).padStart(length, '0')
- // 提取日期组成部分
- const year = dateObj.getFullYear()
- const month = dateObj.getMonth() + 1
- const day = dateObj.getDate()
- const hours = dateObj.getHours()
- const minutes = dateObj.getMinutes()
- const seconds = dateObj.getSeconds()
- const milliseconds = dateObj.getMilliseconds()
- // 替换格式占位符
- return format
- .replace(/YYYY/g, pad(year, 4))
- .replace(/YY/g, pad(year % 100, 2))
- .replace(/MM/g, pad(month))
- .replace(/DD/g, pad(day))
- .replace(/HH/g, pad(hours))
- .replace(/mm/g, pad(minutes))
- .replace(/ss/g, pad(seconds))
- .replace(/SSS/g, pad(milliseconds, 3))
- }
- /**
- * 相对时间格式转换
- * @param {string|Date} date - 日期字符串或Date对象
- * @returns {string} 相对于当前时间的描述(如"3天前")
- */
- export function relativeTime(date) {
- // 处理空值
- if (!date) return ''
- // 创建Date对象
- const dateObj = date instanceof Date ? date : new Date(date)
- // 处理无效日期
- if (isNaN(dateObj.getTime())) {
- console.warn('relativeTime: Invalid date input, returning empty string')
- return ''
- }
- const now = new Date()
- const diffInSeconds = Math.floor((now - dateObj) / 1000)
- const intervals = {
- 年: 31536000,
- 月: 2592000,
- 天: 86400,
- 小时: 3600,
- 分钟: 60
- }
- for (const [unit, seconds] of Object.entries(intervals)) {
- const interval = Math.floor(diffInSeconds / seconds)
- if (interval >= 1) {
- return `${interval}${unit}前`
- }
- }
- return diffInSeconds < 10 ? '刚刚' : `${diffInSeconds}秒前`
- }
- /**
- * 时长格式化(毫秒转可读格式)
- * @param {number} milliseconds - 毫秒数
- * @returns {string} 格式化后的时长(如"01:23:45")
- */
- export function formatDuration(milliseconds) {
- if (!milliseconds && milliseconds !== 0) return ''
- const totalSeconds = Math.floor(milliseconds / 1000)
- const hours = Math.floor(totalSeconds / 3600)
- const minutes = Math.floor((totalSeconds % 3600) / 60)
- const seconds = totalSeconds % 60
- return [hours ? pad(hours) : null, pad(minutes), pad(seconds)]
- .filter(Boolean)
- .join(':')
- }
- // 内部使用的填充函数
- function pad(num) {
- return num.toString().padStart(2, '0')
- }
- // 示例使用
- // console.log(formatDate('2025-08-07T14:33:37.043403')); // 默认格式
- // console.log(formatDate('2025-08-07T14:33:37.043403', 'YYYY年MM月DD日 HH:mm:ss')); // 自定义格式
- // console.log(relativeTime(new Date(Date.now() - 30000))); // 30秒前
- // console.log(formatDuration(12345678)); // 03:25:45
|