index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * 日期格式化工具
  3. * @param {string|Date} date - 日期字符串或Date对象
  4. * @param {string} format - 输出格式,支持以下占位符:
  5. * YYYY - 四位年份
  6. * YY - 两位年份
  7. * MM - 两位月份
  8. * DD - 两位日期
  9. * HH - 24小时制小时
  10. * mm - 分钟
  11. * ss - 秒钟
  12. * SSS - 毫秒
  13. * @returns {string} 格式化后的日期字符串
  14. */
  15. export function formatDate(date, format = 'YYYY-MM-DD HH:mm') {
  16. // 处理空值
  17. if (!date) return ''
  18. // 创建Date对象
  19. const dateObj = date instanceof Date ? date : new Date(date)
  20. // 处理无效日期
  21. if (isNaN(dateObj.getTime())) {
  22. console.warn('formatDate: Invalid date input, returning empty string')
  23. return ''
  24. }
  25. // 填充数字到指定长度
  26. const pad = (num, length = 2) => String(num).padStart(length, '0')
  27. // 提取日期组成部分
  28. const year = dateObj.getFullYear()
  29. const month = dateObj.getMonth() + 1
  30. const day = dateObj.getDate()
  31. const hours = dateObj.getHours()
  32. const minutes = dateObj.getMinutes()
  33. const seconds = dateObj.getSeconds()
  34. const milliseconds = dateObj.getMilliseconds()
  35. // 替换格式占位符
  36. return format
  37. .replace(/YYYY/g, pad(year, 4))
  38. .replace(/YY/g, pad(year % 100, 2))
  39. .replace(/MM/g, pad(month))
  40. .replace(/DD/g, pad(day))
  41. .replace(/HH/g, pad(hours))
  42. .replace(/mm/g, pad(minutes))
  43. .replace(/ss/g, pad(seconds))
  44. .replace(/SSS/g, pad(milliseconds, 3))
  45. }
  46. /**
  47. * 相对时间格式转换
  48. * @param {string|Date} date - 日期字符串或Date对象
  49. * @returns {string} 相对于当前时间的描述(如"3天前")
  50. */
  51. export function relativeTime(date) {
  52. // 处理空值
  53. if (!date) return ''
  54. // 创建Date对象
  55. const dateObj = date instanceof Date ? date : new Date(date)
  56. // 处理无效日期
  57. if (isNaN(dateObj.getTime())) {
  58. console.warn('relativeTime: Invalid date input, returning empty string')
  59. return ''
  60. }
  61. const now = new Date()
  62. const diffInSeconds = Math.floor((now - dateObj) / 1000)
  63. const intervals = {
  64. 年: 31536000,
  65. 月: 2592000,
  66. 天: 86400,
  67. 小时: 3600,
  68. 分钟: 60
  69. }
  70. for (const [unit, seconds] of Object.entries(intervals)) {
  71. const interval = Math.floor(diffInSeconds / seconds)
  72. if (interval >= 1) {
  73. return `${interval}${unit}前`
  74. }
  75. }
  76. return diffInSeconds < 10 ? '刚刚' : `${diffInSeconds}秒前`
  77. }
  78. /**
  79. * 时长格式化(毫秒转可读格式)
  80. * @param {number} milliseconds - 毫秒数
  81. * @returns {string} 格式化后的时长(如"01:23:45")
  82. */
  83. export function formatDuration(milliseconds) {
  84. if (!milliseconds && milliseconds !== 0) return ''
  85. const totalSeconds = Math.floor(milliseconds / 1000)
  86. const hours = Math.floor(totalSeconds / 3600)
  87. const minutes = Math.floor((totalSeconds % 3600) / 60)
  88. const seconds = totalSeconds % 60
  89. return [hours ? pad(hours) : null, pad(minutes), pad(seconds)]
  90. .filter(Boolean)
  91. .join(':')
  92. }
  93. // 内部使用的填充函数
  94. function pad(num) {
  95. return num.toString().padStart(2, '0')
  96. }
  97. // 示例使用
  98. // console.log(formatDate('2025-08-07T14:33:37.043403')); // 默认格式
  99. // console.log(formatDate('2025-08-07T14:33:37.043403', 'YYYY年MM月DD日 HH:mm:ss')); // 自定义格式
  100. // console.log(relativeTime(new Date(Date.now() - 30000))); // 30秒前
  101. // console.log(formatDuration(12345678)); // 03:25:45