date.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import { error } from './throw'
  2. import { weekMap } from '../const'
  3. /**
  4. * 判断这一年是平年还是闰年
  5. * @param {String/Number} year 年份
  6. */
  7. export const isLeapYear = year => (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0)
  8. /**
  9. * 去掉字符串首尾空格
  10. * @param {String} str 字符串
  11. */
  12. export const trimStr = str => str.replace(/^\s*/, '').replace(/\s*$/, '')
  13. /**
  14. * 格式化月份或者天数
  15. * @param {String} dateStr 月份或者天数
  16. */
  17. export const formatMonthOrDay = dateStr => {
  18. if ((`${dateStr}`).length === 2) {
  19. return dateStr
  20. }
  21. return `0${dateStr}`
  22. }
  23. /**
  24. * 获取当前年份
  25. */
  26. export const getCurrentYear = () => new Date().getFullYear()
  27. /**
  28. * 获取当前月份
  29. */
  30. export const getCurrentMonth = () => formatMonthOrDay(new Date().getMonth() + 1)
  31. /**
  32. * 获取当前天数
  33. */
  34. export const getCurrentDay = () => formatMonthOrDay(new Date().getDate())
  35. /**
  36. * 获取当前年份格式
  37. */
  38. export const getCurrentDate = () => `${getCurrentYear()}-${getCurrentMonth()}-${getCurrentDay()}`
  39. /**
  40. * 获取指定月份的天数
  41. * @param {String} year 年份
  42. * @param {String} month 月份
  43. */
  44. export const getDaysCountOfMonth = (month = getCurrentMonth(),
  45. year = getCurrentYear()) => new Date(year, month, 0).getDate()
  46. /**
  47. * 格式化日期
  48. * @param {String} date 日期
  49. */
  50. export const formatDate = date => {
  51. const regexp = /(^(\d{4})(\s*[/\-\\:]\s*)(\d{1,2})(\s*[/\-\\:]\s*)(\d{1,2})$)|(^(\d{4})(\d{1,2})(\d{1,2})$)/
  52. const strArr = trimStr(date).match(regexp)
  53. if (!strArr) {
  54. return { format: '' }
  55. }
  56. const year = strArr[2] || strArr[8]
  57. let month = strArr[4] || strArr[9]
  58. let day = strArr[6] || strArr[10]
  59. if (+month > 12) {
  60. error('month exceed max month number 12')
  61. month = 12
  62. }
  63. const countOfMonth = getDaysCountOfMonth(month, year)
  64. if (+day > countOfMonth) {
  65. error(`day exceed max day number ${countOfMonth}`)
  66. day = countOfMonth
  67. }
  68. month = formatMonthOrDay(month)
  69. day = formatMonthOrDay(day)
  70. return {
  71. format: `${year}-${month}-${day}`,
  72. year: year,
  73. month: month,
  74. day: day,
  75. }
  76. }
  77. /**
  78. * 具体日期适配器
  79. * @param {String} date 日期格式
  80. */
  81. const specificDateAdapter = date => {
  82. const dateObj = formatDate(date)
  83. return flag => dateObj[flag]
  84. }
  85. /**
  86. * 获取指定日期的年份
  87. * @param {String} date 日期格式
  88. */
  89. export const getYearFromSpecificDate = (date = getCurrentDate()) => specificDateAdapter(date)('year')
  90. /**
  91. * 获取指定日期的月份
  92. * @param {String} date 日期格式
  93. */
  94. export const getMonthFromSpecificDate = (date = getCurrentDate()) => specificDateAdapter(date)('month')
  95. /**
  96. * 获取指定日期的天数
  97. * @param {String} date 日期格式
  98. */
  99. export const getDayFromSepecificDate = (date = getCurrentDate()) => specificDateAdapter(date)('day')
  100. /**
  101. * 获取指定日期的年份格式
  102. * @param {String} date 日期格式
  103. */
  104. export const getDateFormatFromSepecificDate = (date = getCurrentDate()) => specificDateAdapter(date)('format')
  105. /**
  106. * 获取一月中的第一天是星期几
  107. * @param {String} month 月份
  108. * @param {String} year 年份
  109. */
  110. export const getWeekOfMonth = (month = getCurrentMonth(), year = getCurrentYear()) => (
  111. new Date(`${year}, ${month}, 01`).getDay()
  112. )
  113. /**
  114. * 获取一月中的第一天是星期几
  115. * @param {String} month 月份
  116. * @param {String} year 年份
  117. */
  118. export const getWeekNameOfMonth = (month = getCurrentMonth(), year = getCurrentYear()) => {
  119. const dayNumber = getWeekOfMonth(month, year)
  120. return weekMap.get(dayNumber)
  121. }
  122. /**
  123. * 是否为当前天
  124. * @param {Sring/Number} year 年份
  125. * @param {Sring/Number} month 月份
  126. * @param {Sring/Number} day 天
  127. */
  128. export const isCurrentDay = (year, month, day) => {
  129. const currentYear = getCurrentYear()
  130. const currentMonth = getCurrentMonth()
  131. const currentDay = getCurrentDay()
  132. /* eslint-disable eqeqeq */
  133. return (currentYear == year) && (currentMonth == month) && (currentDay == day)
  134. }
  135. /**
  136. * 判断日期是否合法
  137. * @param {String} date 日期
  138. */
  139. export const isDateValid = date => {
  140. const regexp = /(^(\d{4})(\s*[/\-\\:]\s*)(\d{1,2})(\s*[/\-\\:]\s*)(\d{1,2})$)|(^(\d{4})(\d{1,2})(\d{1,2})$)/
  141. const strArr = trimStr(date).match(regexp)
  142. if (!strArr) {
  143. return false
  144. }
  145. const year = strArr[2] || strArr[8]
  146. const month = strArr[4] || strArr[9]
  147. const day = strArr[6] || strArr[10]
  148. if (+month > 12) {
  149. return false
  150. }
  151. const countOfMonth = getDaysCountOfMonth(month, year)
  152. if (+day > countOfMonth) {
  153. return false
  154. }
  155. return true
  156. }