xx-calendar.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // components/xx-calendar/xx-calendar.js
  2. import calendarFormatter from "./index";
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. */
  7. properties: {
  8. use_date_arr:{
  9. type:Array,
  10. value:[]
  11. }
  12. },
  13. /**
  14. * 组件的初始数据
  15. */
  16. data: {
  17. // 本月指的是选择的当前月份
  18. year: new Date().getFullYear(),
  19. month: new Date().getMonth() + 1,
  20. weeksArr: ['日', '一', '二', '三', '四', '五', '六'],
  21. nowMonth: new Date().getMonth() + 1, //本月是几月
  22. nowDay: new Date().getDate(), //本月当天的日期
  23. lastMonthDays: [], //上一个月
  24. nowMonthDays: [], //本月
  25. nextMonthDays: [], //下一个月
  26. },
  27. /**
  28. * 组件的方法列表
  29. */
  30. methods: {
  31. //获取当月天数
  32. getThisMonthDays(year, month) {
  33. return new Date(year, month, 0).getDate();
  34. },
  35. /** 总方法 */
  36. //创建日期
  37. createDays(year, month) {
  38. this.getLastMonthDays(year, month)
  39. this.getNowMonthDays(year, month)
  40. this.getNextMonthDays(year, month)
  41. },
  42. /** 获取上个月日期 */
  43. getLastMonthDays(year, month) {
  44. let nowMonthFirstDays = new Date(year, month - 1, 1).getDay()
  45. let lastMonthDays = []
  46. if (nowMonthFirstDays) { //判断当月的第一天是不是星期天
  47. //上个月显示多少天
  48. let lastMonthNums = month - 1 < 0 ? this.getThisMonthDays(year - 1, 12) : this.getThisMonthDays(year, month - 1); //判断是否会跨年
  49. //上个月从几号开始显示
  50. for (let i = lastMonthNums - nowMonthFirstDays + 1; i <= lastMonthNums; i++) {
  51. let time = new Date(year, month - 2, i).toLocaleDateString() //对应的时间
  52. lastMonthDays.push({
  53. date: i, //几号
  54. week: this.data.weeksArr[new Date(year, month - 2, i).getDay()], //星期几
  55. time,
  56. isNowMonthDay: ''
  57. });
  58. }
  59. }
  60. this.setData({
  61. lastMonthDays
  62. })
  63. console.log(lastMonthDays);
  64. },
  65. /** 获取当月日期 */
  66. getNowMonthDays(year, month) {
  67. let {
  68. nowMonth,
  69. nowDay
  70. } = this.data
  71. let nowMonthDays = []
  72. let days = this.getThisMonthDays(year, month); //获取当月的天数
  73. for (let i = 1; i <= days; i++) {
  74. let d = new Date(year, month - 1, i)
  75. let years = d.getFullYear()
  76. let months = d.getMonth() + 1
  77. let day2 = d.getDate()
  78. let time = `${years+'/'+months +'/'+day2}` // 2022/3/3
  79. let timer = time.replace(/\//g, "-")
  80. let timer2 = timer.split('-')
  81. var day = calendarFormatter.solar2lunar(timer2[0], timer2[1], timer2[2]);
  82. let newdate
  83. if (day.IDayCn == '初一') {
  84. newdate = day.IMonthCn
  85. } else {
  86. newdate = day.IDayCn
  87. }
  88. nowMonthDays.push({
  89. date: i, //几号
  90. week: this.data.weeksArr[new Date(year, month - 1, i).getDay()], //星期几
  91. time,
  92. color: false,
  93. day: newdate,
  94. isNowMonthDay: (month == nowMonth && i == nowDay) ? "isNowMonthDay" : ""
  95. });
  96. }
  97. this.data.use_date_arr.forEach(ele => {
  98. ele = ele.replace(/\-/g, "/")
  99. nowMonthDays.forEach(item => {
  100. if (ele == item.time) {
  101. console.log(item);
  102. item.color = true
  103. }
  104. })
  105. })
  106. this.setData({
  107. nowMonthDays
  108. })
  109. console.log(nowMonthDays);
  110. },
  111. /** 获取下个月日期 */
  112. getNextMonthDays(year, month) {
  113. let {
  114. lastMonthDays,
  115. nowMonthDays,
  116. } = this.data
  117. let nextMonthDays = []
  118. let nextMonthNums = (lastMonthDays.length + nowMonthDays.length) > 35 ? 42 - (lastMonthDays.length + nowMonthDays.length) : 35 - (lastMonthDays.length + nowMonthDays.length) //下个月显示多少天
  119. let nowYear = (parseInt(month) + 1) > 12 ? year + 1 : year //下一个月的年份
  120. let nowMonth = (parseInt(month) + 1) > 12 ? 1 : parseInt(month) + 1 //下一个月的月份
  121. if (nextMonthNums) { //判断当前天数是否大于零
  122. for (let i = 1; i <= nextMonthNums; i++) {
  123. let time = new Date(year, month - 1, i).toLocaleDateString()
  124. nextMonthDays.push({
  125. date: i, //几号
  126. week: this.data.weeksArr[new Date(nowYear, nowMonth - 1, i).getDay()], //星期几
  127. time,
  128. isNowMonthDay: ''
  129. });
  130. }
  131. }
  132. this.setData({
  133. nextMonthDays
  134. })
  135. console.log(nextMonthDays)
  136. },
  137. /** 切换月份 */
  138. changeMonthFun(e){
  139. let {
  140. year,
  141. month
  142. } = this.data
  143. let type = e.currentTarget.dataset.type //类型
  144. if (type == 'prev') { //上一个月
  145. year = month - 1 > 0 ? year : year - 1
  146. month = month - 1 > 0 ? month - 1 : 12
  147. } else { //next 下个月
  148. year = (parseInt(month) + 1) > 12 ? year + 1 : year
  149. month = (parseInt(month) + 1) > 12 ? 1 : parseInt(month) + 1
  150. }
  151. this.setData({
  152. year,
  153. month,
  154. })
  155. this.createDays(year, month)
  156. },
  157. /** 选择日期触发 */
  158. selectDate(e){
  159. let type = e.currentTarget.dataset.type //选择的时间类型
  160. let index = e.currentTarget.dataset.index //选择的下标
  161. let date = e.currentTarget.dataset.item.time //选择的下标
  162. let selectDate = date.replace(/\//g, "-")
  163. console.log("选择的时间", selectDate)
  164. // 自定义事件,父组件调用,回调 选择的时间selectDate
  165. this.triggerEvent('selectDate', selectDate)
  166. //将选择的时间类型的 isNowMonthDay 全改为''
  167. this.data[type]?.forEach(item => {
  168. item.isNowMonthDay = ''
  169. })
  170. this.data[type]?.forEach((item, idx) => {
  171. if (index == idx) {
  172. item.isNowMonthDay = (item.time == new Date().toLocaleDateString() ? "isNowMonthDay" : "isNotNowMonthDay"); //判断当前选中的日期是否是当前时间
  173. } else {
  174. item.isNowMonthDay = ''
  175. }
  176. })
  177. this.setData({
  178. [type]: this.data[type],
  179. })
  180. },
  181. },
  182. ready() {
  183. let {
  184. year,
  185. month
  186. } = this.data
  187. this.createDays(year, month)
  188. }
  189. })