calendar.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. },
  22. ready() {
  23. this.getMonthDaysCurrent(new Date())
  24. },
  25. /**
  26. * 组件的方法列表
  27. */
  28. methods: {
  29. // 所选时间对应月份日期
  30. getMonthDaysCurrent(e) {
  31. let year = e.getFullYear()
  32. let month = e.getMonth() + 1
  33. let date = e.getDate()
  34. let day = e.getDay() // 周几
  35. let days = new Date(year, month, 0).getDate() //当月天数(即下个月0号=当月最后一天)
  36. let firstDayDate = new Date(year, month - 1, 1) // 当月1号
  37. let firstDay = firstDayDate.getDay() //当月1号对应的星期
  38. let lastDate = new Date(year, month - 1, days) //当月最后一天日期
  39. let lastDay = lastDate.getDay() //当月最后一天对应的星期
  40. let calendarDays = []
  41. // 上个月显示的天数及日期
  42. for (let i = firstDay - 2; i >= 0; i--) {
  43. let date = new Date(year, month - 1, -i)
  44. //console.log(date, date.getMonth() + 1)
  45. calendarDays.push({
  46. 'year': date.getFullYear(),
  47. 'month': date.getMonth() + 1,
  48. 'date': date.getDate(),
  49. 'day': date.getDay(),
  50. 'current': false,
  51. })
  52. }
  53. // 当月显示的日期
  54. for (let i = 1; i <= days; i++) {
  55. let index = calendarDays.push({
  56. 'year': year,
  57. 'month': month,
  58. 'date': i,
  59. 'day': new Date(year, month - 1, i).getDay(),
  60. 'current': true,
  61. })
  62. if (i == date) {
  63. this.setData({
  64. selectedIndex: index - 1,
  65. selectedDay: calendarDays[index - 1]
  66. })
  67. console.log(calendarDays[index - 1])
  68. }
  69. }
  70. // 下个月显示的天数及日期
  71. for (let i = 1; i <= 7 - lastDay; i++) {
  72. let date = new Date(year, month, i)
  73. calendarDays.push({
  74. 'year': date.getFullYear(),
  75. 'month': date.getMonth() + 1,
  76. 'date': date.getDate(),
  77. 'day': date.getDay(),
  78. 'current': false,
  79. })
  80. }
  81. this.setData({
  82. calendarDays,
  83. month
  84. })
  85. },
  86. // 手动选中日期
  87. clickDate(e) {
  88. let index = e.currentTarget.dataset.index
  89. let list = this.data.calendarDays
  90. if (list[index].current) {
  91. this.triggerEvent('day', list[index])
  92. this.setData({
  93. calendarDays: list,
  94. selectedIndex: index,
  95. selectedDay: list[index]
  96. })
  97. }
  98. },
  99. next() {
  100. this.getMonthDaysCurrent(new Date(this.data.selectedDay.year, this.data.selectedDay.month, 1))
  101. },
  102. last() {
  103. this.getMonthDaysCurrent(new Date(this.data.selectedDay.year, this.data.selectedDay.month - 2, 1))
  104. }
  105. }
  106. })