dateUtil.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. const weekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
  2. // 格式化日期为 YYYY-MM-DD
  3. export function formatToMonthDay(date) {
  4. const month = String(date.getMonth() + 1).padStart(2, '0')
  5. const day = String(date.getDate()).padStart(2, '0')
  6. return `${month}月${day}日`
  7. }
  8. /**
  9. * 根据传入时间获取星期几
  10. * @param {Date} date
  11. */
  12. export function getWeekday(date) {
  13. return weekdays[date.getDay()]
  14. }
  15. /**
  16. * 判断两个时间是否是同一天
  17. * @param {Object} date1
  18. * @param {Object} date2
  19. */
  20. export function isSameDay(date1, date2) {
  21. // 检查两个日期是否为同一天
  22. return date1.getFullYear() === date2.getFullYear() &&
  23. date1.getMonth() === date2.getMonth() &&
  24. date1.getDate() === date2.getDate()
  25. }
  26. // 格式化日期为 YYYY-MM-DD
  27. const formatDate = (date) => {
  28. const year = date.getFullYear()
  29. const month = String(date.getMonth() + 1).padStart(2, '0')
  30. const day = String(date.getDate()).padStart(2, '0')
  31. return `${year}-${month}-${day}`
  32. }
  33. /**
  34. * 根据传入时间, 获取传入时间的本周的起止日期(结束日期为昨天)
  35. * @param {Date} inputDate 传入的时间
  36. */
  37. export function getCurrentWeekDates(inputDate) {
  38. const dayOfWeek = inputDate.getDay() // 0 (周日) 到 6 (周六)
  39. // 计算到本周一的日期差
  40. // 如果今天是周一(1),则 diffToMonday = 0
  41. // 如果今天是周日(0),则 diffToMonday = 6
  42. const diffToMonday = dayOfWeek === 0 ? 6 : dayOfWeek - 1
  43. // 获取本周一(本周的第一天)
  44. const monday = new Date(inputDate)
  45. monday.setDate(inputDate.getDate() - diffToMonday)
  46. // 获取昨天的日期
  47. const yesterday = new Date(inputDate)
  48. // yesterday.setDate(inputDate.getDate() - 1)
  49. return {
  50. start: formatDate(monday),
  51. end: formatDate(yesterday)
  52. }
  53. }
  54. /**
  55. * 根据传入时间, 获取传入时间的上一周的起止日期
  56. * @param {Date} inputDate 传入的时间
  57. */
  58. export function getLastWeekDates(inputDate) {
  59. const dayOfWeek = inputDate.getDay() // 0 (周日) 到 6 (周六)
  60. // 计算到上周一的日期差
  61. // 如果今天是周一(1),则diffToLastWeek = 1 + 7 = 8(回到上周一)
  62. // 如果今天是周日(0),则diffToLastWeek = 0 + 7 + 1 = 8(回到上周一)
  63. const diffToLastWeek = dayOfWeek === 0 ? 7 : dayOfWeek - 1 + 7
  64. // 获取上周一(上周的第一天)
  65. const lastMonday = new Date(inputDate)
  66. lastMonday.setDate(inputDate.getDate() - diffToLastWeek)
  67. // 获取上周日(上周的最后一天)
  68. const lastSunday = new Date(lastMonday)
  69. lastSunday.setDate(lastMonday.getDate() + 6)
  70. return {
  71. start: formatDate(lastMonday),
  72. end: formatDate(lastSunday)
  73. }
  74. }
  75. /**
  76. * 根据传入时间, 获取传入时间的下一周的起止日期, 结束日期不能超过今天, 如果超过今天, 则结束日期是今天
  77. * @param {Date} inputDate 传入的时间
  78. */
  79. export function getNextWeekDates(inputDate) {
  80. const dayOfWeek = inputDate.getDay() // 0 (周日) 到 6 (周六)
  81. // 计算到下周一的日期差
  82. // 如果今天是周一(1),则 diffToNextMonday = 7
  83. // 如果今天是周日(0),则 diffToNextMonday = 1
  84. const diffToNextMonday = dayOfWeek === 0 ? 1 : 7 - dayOfWeek + 1
  85. // 获取下周一(下一周的第一天)
  86. const nextMonday = new Date(inputDate)
  87. nextMonday.setDate(inputDate.getDate() + diffToNextMonday)
  88. // 获取下一周的结束日期(下周一加6天)
  89. const nextSunday = new Date(nextMonday)
  90. nextSunday.setDate(nextMonday.getDate() + 6)
  91. // 确保结束日期不超过今天
  92. const today = new Date()
  93. today.setHours(0, 0, 0, 0) // 设置为今天的午夜,以便比较
  94. if (nextSunday > today) {
  95. nextSunday.setTime(today.getTime()) // 如果超过今天,则设置为今天
  96. }
  97. return {
  98. start: formatDate(nextMonday),
  99. end: formatDate(nextSunday)
  100. }
  101. }
  102. /**
  103. * 根据传入时间, 获取传入时间的当月的起止日期(结束时间为当前日期)
  104. * @param {Date} inputDate 传入的时间
  105. */
  106. export function getMonthlyDates(inputDate) {
  107. // 获取传入日期所在月份的第一天
  108. const firstDayOfMonth = new Date(inputDate.getFullYear(), inputDate.getMonth(), 1)
  109. // 获取昨天的日期
  110. const yesterday = new Date(inputDate)
  111. // yesterday.setDate(inputDate.getDate() - 1)
  112. return {
  113. start: formatDate(firstDayOfMonth),
  114. end: formatDate(yesterday)
  115. }
  116. }
  117. /**
  118. * 根据传入时间, 获取当前时间的上月的起止日期
  119. * @param {Date} inputDate 传入的时间
  120. */
  121. export function getLastMonthDates(inputDate) {
  122. // 获取传入日期所在月份的第一天
  123. const firstDayOfCurrentMonth = new Date(inputDate.getFullYear(), inputDate.getMonth(), 1)
  124. // 获取上个月的最后一天
  125. const lastDayOfLastMonth = new Date(firstDayOfCurrentMonth - 1)
  126. // 获取上个月的第一天
  127. const firstDayOfLastMonth = new Date(lastDayOfLastMonth.getFullYear(), lastDayOfLastMonth.getMonth(), 1)
  128. return {
  129. start: formatDate(firstDayOfLastMonth),
  130. end: formatDate(lastDayOfLastMonth)
  131. }
  132. }
  133. /**
  134. * 根据传入时间, 获取传入时间的下一月的起止日期, 结束日期不能超过今天, 如果超过今天, 则结束日期是今天
  135. * @param {Date} inputDate 传入的时间
  136. */
  137. export function getNextMonthDates(inputDate) {
  138. // 获取下一月的第一天
  139. const nextMonthFirstDay = new Date(inputDate.getFullYear(), inputDate.getMonth() + 1, 1)
  140. // 获取下一月的最后一天
  141. const nextMonthLastDay = new Date(nextMonthFirstDay.getFullYear(), nextMonthFirstDay.getMonth() + 1, 0)
  142. // 确保结束日期不超过今天
  143. const today = new Date()
  144. today.setHours(0, 0, 0, 0) // 设置为今天的午夜,以便比较
  145. if (nextMonthLastDay > today) {
  146. nextMonthLastDay.setTime(today.getTime()) // 如果超过今天,则设置为今天
  147. }
  148. return {
  149. start: formatDate(nextMonthFirstDay),
  150. end: formatDate(nextMonthLastDay)
  151. }
  152. }