date.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // 时间戳转换为年月日时分秒
  2. export const timesTampChange = (timestamp, format = 'Y-M-D h:m:s') => {
  3. if (!timestamp) return ''
  4. const date = new Date(timestamp)
  5. const Y = date.getFullYear().toString()
  6. const M = (date.getMonth() + 1).toString().padStart(2, '0')
  7. const D = date.getDate().toString().padStart(2, '0')
  8. const h = date.getHours().toString().padStart(2, '0')
  9. const m = date.getMinutes().toString().padStart(2, '0')
  10. const s = date.getSeconds().toString().padStart(2, '0')
  11. const formatter = { 'Y': Y, 'M': M, 'D': D, 'h': h, 'm': m, 's': s } // 替换format中的占位符
  12. let formattedDate = format.replace(/Y|M|D|h|m|s/g, matched => formatter[matched]) // 使用正则表达式匹配并替换占位符
  13. if (!formattedDate) formattedDate = Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s
  14. return formattedDate
  15. }
  16. // 根据生日时间戳计算年龄
  17. export const getAgeByBirthdayTimestamp = (timestamp) => {
  18. const now = new Date()
  19. const birthday = new Date(timestamp)
  20. const age = now.getFullYear() - birthday.getFullYear()
  21. return age
  22. }
  23. // 将YYYY-MM转换为时间戳
  24. export const convertYearMonthToTimestamp = (yearMonth) => {
  25. const regex = /^\d{4}-\d{2}$/
  26. if (!regex.test(yearMonth)) {
  27. throw new Error("Invalid input format. Expected 'YYYY-MM'.")
  28. }
  29. const [year, month] = yearMonth.split('-').map(Number)
  30. const date = new Date(Date.UTC(year, month - 1, 1))
  31. const timestamp = date.getTime()
  32. return timestamp
  33. }
  34. // 法将 YYYY-MM-DD、YYYY-MM 或 YYYY 格式的日期转换为时间戳
  35. export const dateToTimestamp = (dateStr) => {
  36. if (!dateStr) return null
  37. if (dateStr.length === 4) {
  38. dateStr += '-01-01' // YYYY -> YYYY-01-01
  39. } else if (dateStr.length === 7) {
  40. dateStr += '-01' // YYYY-MM -> YYYY-MM-01
  41. }
  42. return new Date(dateStr).getTime()
  43. }
  44. export const getTimeDifferenceInChinese = (startTime, endTime) => {
  45. // 将时间戳转换为 Date 对象(假设时间戳单位为毫秒)
  46. const startDate = startTime ? new Date(startTime) : new Date();
  47. const endDate = endTime ? new Date(endTime) : new Date();
  48. // 计算年份差
  49. let yearsDiff = endDate.getFullYear() - startDate.getFullYear();
  50. // 计算月份差(考虑年份差后调整)
  51. let monthsDiff = endDate.getMonth() - startDate.getMonth();
  52. // 如果月份差为负,则从上一年借月
  53. if (monthsDiff < 0) {
  54. yearsDiff--;
  55. monthsDiff += 12;
  56. }
  57. // 计算日期差(考虑月份差后调整,如果日期差为负,则从上一月借天)
  58. let daysDiff = endDate.getDate() - startDate.getDate();
  59. if (daysDiff < 0) {
  60. monthsDiff--;
  61. // 获取 startDate 所在月的最后一天
  62. const lastDayOfMonth = new Date(startDate.getFullYear(), startDate.getMonth() + 1, 0).getDate();
  63. daysDiff += lastDayOfMonth; // 加上最后一天以补全月份差
  64. }
  65. // 构建结果字符串
  66. let result = "";
  67. if (yearsDiff > 0) {
  68. result += `${yearsDiff}年`;
  69. }
  70. if (monthsDiff > 0) {
  71. if (result) {
  72. // 如果已经有年份差异,则直接添加月份数(不带单位),后面正则替换会处理
  73. result += monthsDiff;
  74. } else {
  75. // 如果没有年份差异,则正常添加月份和单位
  76. result += `${monthsDiff}个月`;
  77. // 特别处理只有1个月的情况
  78. if (monthsDiff === 1) {
  79. result = "不到1个月"; // 直接替换为“不到1个月”,避免后续复杂的正则替换
  80. }
  81. }
  82. } else if (!result && daysDiff >= 0) {
  83. // 如果没有年份和月份差异,但天数差异存在(这里其实没处理天数,只是为完整性添加)
  84. // 理论上应该处理天数,但题目要求只到月份,所以这里直接返回“不到1个月”
  85. result = "不到1个月";
  86. }
  87. // 如果之前添加了月份数但没有年份(且不是直接处理的1个月情况),则需要去除末尾多余的数字并添加“个月”
  88. if (result && !/\d年$/.test(result) && /\d$/.test(result)) {
  89. result += "个月";
  90. }
  91. return result
  92. }
  93. // 设置面试邀请默认时间
  94. export const getInterviewInviteDefaultTime = () => {
  95. const today = new Date()
  96. today.setDate(today.getDate() + 1)
  97. const time = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 10, 0, 0)
  98. return {
  99. time,
  100. timeStamp: time.getTime()
  101. }
  102. }
  103. export const getNextDate = (createTimestamp, format = 'YYYY-MM-DD', type, startTime) => {
  104. if (type === 'day') createTimestamp = createTimestamp * 24 * 60 * 60 * 1000
  105. if (type === 'hour') createTimestamp = createTimestamp * 60 * 60 * 1000
  106. let date = new Date(startTime ? startTime + createTimestamp : new Date().getTime() + createTimestamp)
  107. let year = date.getFullYear()
  108. let month = date.getMonth() + 1
  109. let day = date.getDate()
  110. month = month < 10 ? `0${month}` : month
  111. day = day < 10 ? `0${day}` : day
  112. if (format === 'YYYY-MM') {
  113. return `${year}-${month}`
  114. } else if (format === 'YYYY-MM-DD') {
  115. return `${year}-${month}-${day}`
  116. } else {
  117. return `${year}-${month}-${day}` // 默认返回 yyyy-mm-dd
  118. }
  119. }