calendar.js 3.7 KB

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