calendar.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // pages/clock/clock-tongji/calendar/calendar.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. list: {
  8. type: Array,
  9. value: []
  10. },
  11. },
  12. /**
  13. * 组件的初始数据
  14. */
  15. data: {
  16. weeks: ["一", "二", "三", "四", "五", "六", "日"],
  17. selectedIndex: '',
  18. selectedDay: {},
  19. // 日期list
  20. calendarDays: [],
  21. defaultMonth: '',
  22. defaultDate: '',
  23. blue: ''
  24. },
  25. ready() {
  26. },
  27. /**
  28. * 组件的方法列表
  29. */
  30. methods: {
  31. init(e) {
  32. this.setData({
  33. defaultMonth: e.getMonth() + 1,
  34. defaultDate: e.getDate(),
  35. })
  36. this.getMonthDaysCurrent(e)
  37. },
  38. // 所选时间对应月份日期
  39. getMonthDaysCurrent(e) {
  40. let year = e.getFullYear()
  41. let month = e.getMonth() + 1
  42. let date = e.getDate()
  43. let day = e.getDay() // 周几
  44. let days = new Date(year, month, 0).getDate() //当月天数(即下个月0号=当月最后一天)
  45. let firstDayDate = new Date(year, month - 1, 1) // 当月1号
  46. let firstDay = firstDayDate.getDay() //当月1号对应的星期
  47. let lastDate = new Date(year, month - 1, days) //当月最后一天日期
  48. let lastDay = lastDate.getDay() //当月最后一天对应的星期
  49. let calendarDays = []
  50. this.setData({
  51. selectedIndex: '',
  52. blue: ''
  53. })
  54. // 上个月显示的天数及日期
  55. for (let i = firstDay - 2; i >= 0; i--) {
  56. let date = new Date(year, month - 1, -i)
  57. //console.log(date, date.getMonth() + 1)
  58. calendarDays.push({
  59. 'year': date.getFullYear(),
  60. 'month': date.getMonth() + 1,
  61. 'date': date.getDate(),
  62. 'day': date.getDay(),
  63. 'current': false,
  64. })
  65. }
  66. // 当月显示的日期
  67. for (let i = 1; i <= days; i++) {
  68. let index = calendarDays.push({
  69. 'year': year,
  70. 'month': month,
  71. 'date': i,
  72. 'day': new Date(year, month - 1, i).getDay(),
  73. 'current': true,
  74. })
  75. if (i == date) {
  76. this.setData({
  77. selectedDay: calendarDays[index - 1]
  78. })
  79. }
  80. if (month === this.data.defaultMonth && i === this.data.defaultDate) {
  81. this.setData({
  82. selectedIndex: index - 1,
  83. blue: index - 1,
  84. })
  85. }
  86. }
  87. // 下个月显示的天数及日期
  88. for (let i = 1; i <= 7 - lastDay; i++) {
  89. let date = new Date(year, month, i)
  90. calendarDays.push({
  91. 'year': date.getFullYear(),
  92. 'month': date.getMonth() + 1,
  93. 'date': date.getDate(),
  94. 'day': date.getDay(),
  95. 'current': false,
  96. })
  97. }
  98. this.setData({
  99. calendarDays,
  100. month
  101. })
  102. },
  103. // 手动选中日期
  104. clickDate(e) {
  105. let index = e.currentTarget.dataset.index
  106. let list = this.data.calendarDays
  107. if (list[index].current) {
  108. this.triggerEvent('day', list[index])
  109. this.setData({
  110. calendarDays: list,
  111. selectedIndex: index,
  112. selectedDay: list[index],
  113. selectedIndex: index,
  114. blue: index,
  115. defaultMonth: list[index].month,
  116. defaultDate: list[index].date
  117. })
  118. }
  119. },
  120. next() {
  121. this.getMonthDaysCurrent(new Date(this.data.selectedDay.year, this.data.selectedDay.month, 1))
  122. },
  123. last() {
  124. this.getMonthDaysCurrent(new Date(this.data.selectedDay.year, this.data.selectedDay.month - 2, 1))
  125. }
  126. }
  127. })