index.uts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { Lunar, LunarInfoType } from './calendar.uts'
  2. export type DateType = {
  3. fullDate : string;
  4. year : number;
  5. month : number;
  6. date : number;
  7. day : number;
  8. disabled : boolean;
  9. lunar : string;
  10. is_today : boolean;
  11. data ?: LunarInfoType
  12. }
  13. export class Calendar {
  14. private lunar:Lunar
  15. constructor() {
  16. this.lunar =new Lunar()
  17. }
  18. getDateInfo(time : string = '') : DateType {
  19. const nowDate = this.getDate(time)
  20. const lunar = this.getlunar(nowDate.year, nowDate.month, nowDate.date)
  21. const item : DateType = nowDate
  22. item.data = lunar
  23. return item
  24. }
  25. /**
  26. * 获取每周数据
  27. * @param {Object} dateData
  28. */
  29. getWeeks(dateData : string = '') : Array<Array<DateType>> {
  30. const dateObj = this.getDate(dateData)
  31. const year = dateObj.year
  32. const month = dateObj.month
  33. let firstDay = new Date(year, month - 1, 0).getDay()
  34. // 获取本月天数
  35. let currentDay = new Date(year, month, 0).getDate()
  36. // 上个月末尾几天
  37. const lastMonthDays = this._getLastMonthDays(firstDay, dateObj)
  38. // 本月天数
  39. const currentMonthDys = this._currentMonthDys(currentDay, dateObj)
  40. // 本月剩余天数
  41. const surplus = 42 - (lastMonthDays.length + currentMonthDys.length)
  42. // 下个月开始几天
  43. const nextMonthDays = this._getNextMonthDays(surplus, dateObj)
  44. // const weeks = []
  45. // 本月所有日期格子合并
  46. let days : Array<DateType> = []
  47. for (let i = 0; i < lastMonthDays.length; i++) {
  48. const item = lastMonthDays[i]
  49. days.push(item)
  50. }
  51. for (let i = 0; i < currentMonthDys.length; i++) {
  52. const item = currentMonthDys[i]
  53. days.push(item)
  54. }
  55. for (let i = 0; i < nextMonthDays.length; i++) {
  56. const item = nextMonthDays[i]
  57. days.push(item)
  58. }
  59. let weeks : Array<Array<DateType>> = []
  60. // 拼接数组 上个月开始几天 + 本月天数+ 下个月开始几天
  61. for (let i = 0; i < days.length; i += 7) {
  62. const item : Array<DateType> = days.slice(i, i + 7)
  63. weeks.push(item);
  64. }
  65. return weeks
  66. }
  67. /**
  68. * 获取上月剩余天数
  69. */
  70. _getLastMonthDays(firstDay : number, full : DateType) : Array<DateType> {
  71. let dateArr : Array<DateType> = []
  72. for (let i = firstDay; i > 0; i--) {
  73. const month = full.month - 1
  74. const beforeDate = new Date(full.year, month, -i + 1).getDate()
  75. let nowDate = full.year + '-' + month + '-' + beforeDate
  76. let item : DateType = this.getDate(nowDate)
  77. item.disabled = true
  78. dateArr.push(item)
  79. }
  80. return dateArr
  81. }
  82. /**
  83. * 获取本月天数
  84. */
  85. _currentMonthDys(dateData : number, full : DateType) : Array<DateType> {
  86. let dateArr : Array<DateType> = []
  87. for (let i = 1; i <= dateData; i++) {
  88. let nowDate = full.year + '-' + full.month + '-' + i
  89. let item : DateType = this.getDate(nowDate)
  90. item.disabled = false
  91. dateArr.push(item)
  92. }
  93. return dateArr
  94. }
  95. /**
  96. * 获取下月天数
  97. */
  98. _getNextMonthDays(surplus : number, full : DateType) : Array<DateType> {
  99. let dateArr : Array<DateType> = []
  100. for (let i = 1; i < surplus + 1; i++) {
  101. const month = full.month + 1
  102. let nowDate = full.year + '-' + month + '-' + i
  103. let item : DateType = this.getDate(nowDate)
  104. item.disabled = true
  105. dateArr.push(item)
  106. }
  107. return dateArr
  108. }
  109. /**
  110. * 计算阴历日期显示
  111. */
  112. getlunar(year : number, month : number, date : number) : LunarInfoType {
  113. return this.lunar.solar2lunar(year, month, date)
  114. }
  115. /**
  116. * 获取任意时间
  117. */
  118. getDate(date : string = '', AddDayCount : number = 0, str : string = 'day') : DateType {
  119. let dd : Date = new Date()
  120. if (date !== '') {
  121. const datePart = date.split(" ");
  122. const dateData = datePart[0].split("-");
  123. const year = parseInt(dateData[0])
  124. const month = parseInt(dateData[1])
  125. const day = parseInt(dateData[2])
  126. dd = new Date(year, month - 1, day)
  127. }
  128. switch (str) {
  129. case 'day':
  130. dd.setDate(dd.getDate() + AddDayCount);
  131. break;
  132. case 'month':
  133. dd.setMonth(dd.getMonth() + AddDayCount);
  134. break;
  135. case 'year':
  136. dd.setFullYear(dd.getFullYear() + AddDayCount);
  137. break;
  138. }
  139. const y = dd.getFullYear();
  140. const m = dd.getMonth() + 1;
  141. const d = dd.getDate();
  142. let nowDate = y + '-' + m + '-' + d
  143. const lunarData = this.getlunar(y, m, d)
  144. const dataObj : DateType = {
  145. fullDate: nowDate,
  146. year: y,
  147. month: m,
  148. date: d,
  149. day: dd.getDay() + 1,
  150. lunar: lunarData.IDayCn,
  151. is_today: lunarData.isToday,
  152. disabled: false
  153. }
  154. return dataObj
  155. }
  156. }