date.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import warnning from './warning'
  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} date 日期
  42. */
  43. export const formatDate = date => {
  44. const regexp = /(^(\d{4})(\s*[/\-\\:]\s*)(\d{1,2})(\s*[/\-\\:]\s*)(\d{1,2})$)|(^(\d{4})(\d{1,2})(\d{1,2})$)/
  45. const strArr = trimStr(date).match(regexp)
  46. if (!strArr) {
  47. return { format: '' }
  48. }
  49. const year = strArr[2] || strArr[8]
  50. let month = strArr[4] || strArr[9]
  51. let day = strArr[6] || strArr[10]
  52. if (+month > 12) {
  53. warnning('month exceed max month number 12')
  54. month = 12
  55. }
  56. if (+day > 31) {
  57. warnning('day exceed max day number 31')
  58. day = 31
  59. }
  60. month = formatMonthOrDay(month)
  61. day = formatMonthOrDay(day)
  62. return {
  63. format: `${year}-${month}-${day}`,
  64. year: year,
  65. month: month,
  66. day: day,
  67. }
  68. }
  69. /**
  70. * 具体日期适配器
  71. * @param {String} date 日期格式
  72. */
  73. const specificDateAdapter = date => {
  74. const dateObj = formatDate(date)
  75. return flag => dateObj[flag]
  76. }
  77. /**
  78. * 获取指定日期的年份
  79. * @param {String} date 日期格式
  80. */
  81. export const getYearFromSpecificDate = (date = getCurrentDate()) => specificDateAdapter(date)('year')
  82. /**
  83. * 获取指定日期的月份
  84. * @param {String} date 日期格式
  85. */
  86. export const getMonthFromSpecificDate = (date = getCurrentDate()) => specificDateAdapter(date)('month')
  87. /**
  88. * 获取指定日期的天数
  89. * @param {String} date 日期格式
  90. */
  91. export const getDayFromSepecificDate = (date = getCurrentDate()) => specificDateAdapter(date)('day')
  92. /**
  93. * 获取指定日期的年份格式
  94. * @param {String} date 日期格式
  95. */
  96. export const getDateFormatFromSepecificDate = (date = getCurrentDate()) => specificDateAdapter(date)('format')
  97. /**
  98. * 获取指定月份的天数
  99. * @param {String} year 年份
  100. * @param {String} month 月份
  101. */
  102. export const getDaysCountOfMonth = (month = getCurrentMonth(),
  103. year = getCurrentYear()) => new Date(year, month, 0).getDate()
  104. /**
  105. * 获取一月中的第一天是星期几
  106. * @param {String} month 月份
  107. * @param {String} year 年份
  108. */
  109. export const getWeekOfMonth = (month = getCurrentMonth(), year = getCurrentYear()) => (
  110. new Date(`${year}, ${month}, 01`).getDay()
  111. )
  112. /**
  113. * 获取一月中的第一天是星期几
  114. * @param {String} month 月份
  115. * @param {String} year 年份
  116. */
  117. export const getWeekNameOfMonth = (month = getCurrentMonth(), year = getCurrentYear()) => {
  118. const dayNumber = getWeekOfMonth(month, year)
  119. return weekMap.get(dayNumber)
  120. }
  121. /**
  122. * 是否为当前天
  123. * @param {Sring/Number} year 年份
  124. * @param {Sring/Number} month 月份
  125. * @param {Sring/Number} day 天
  126. */
  127. export const isCurrentDay = (year, month, day) => {
  128. const currentYear = getCurrentYear()
  129. const currentMonth = getCurrentMonth()
  130. const currentDay = getCurrentDay()
  131. /* eslint-disable eqeqeq */
  132. return (currentYear == year) && (currentMonth == month) && (currentDay == day)
  133. }
  134. /**
  135. * 判断日期是否合法
  136. * @param {String} date 日期
  137. */
  138. export const isDateValid = date => {
  139. const regexp = /(^(\d{4})(\s*[/\-\\:]\s*)(\d{1,2})(\s*[/\-\\:]\s*)(\d{1,2})$)|(^(\d{4})(\d{1,2})(\d{1,2})$)/
  140. const strArr = trimStr(date).match(regexp)
  141. if (!strArr) {
  142. return false
  143. }
  144. const year = strArr[2] || strArr[8]
  145. const month = strArr[4] || strArr[9]
  146. const day = strArr[6] || strArr[10]
  147. if (+month > 12) {
  148. return false
  149. }
  150. const countOfMonth = getDaysCountOfMonth(month, year)
  151. if (+day > countOfMonth) {
  152. return false
  153. }
  154. return true
  155. }