| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- // pages/clock/clock-tongji/calendar/calendar.js
- let first = true
- Component({
- /**
- * 组件的属性列表
- */
- properties: {
- list: {
- type: Array,
- value: []
- },
- },
- /**
- * 组件的初始数据
- */
- data: {
- weeks: ["一", "二", "三", "四", "五", "六", "日"],
- selectedIndex: '',
- selectedDay: {},
- // 日期list
- calendarDays: [],
- defaultMonth: '',
- defaultDate: '',
- blue: ''
- },
- ready() {
- },
- /**
- * 组件的方法列表
- */
- methods: {
- init(e) {
- this.setData({
- defaultMonth: e.getMonth() + 1,
- defaultDate: e.getDate(),
- })
- this.getMonthDaysCurrent(e)
- },
- // 所选时间对应月份日期
- getMonthDaysCurrent(e) {
- let year = e.getFullYear()
- let month = e.getMonth() + 1
- let date = e.getDate()
- let day = e.getDay() // 周几
- let days = new Date(year, month, 0).getDate() //当月天数(即下个月0号=当月最后一天)
- let firstDayDate = new Date(year, month - 1, 1) // 当月1号
- let firstDay = firstDayDate.getDay() //当月1号对应的星期
- let lastDate = new Date(year, month - 1, days) //当月最后一天日期
- let lastDay = lastDate.getDay() //当月最后一天对应的星期
- let calendarDays = []
- this.setData({
- selectedIndex: '',
- blue: ''
- })
- // 上个月显示的天数及日期
- for (let i = firstDay - 2; i >= 0; i--) {
- let date = new Date(year, month - 1, -i)
- //console.log(date, date.getMonth() + 1)
- calendarDays.push({
- 'year': date.getFullYear(),
- 'month': date.getMonth() + 1,
- 'date': date.getDate(),
- 'day': date.getDay(),
- 'current': false,
- })
- }
- // 当月显示的日期
- for (let i = 1; i <= days; i++) {
- let index = calendarDays.push({
- 'year': year,
- 'month': month,
- 'date': i,
- 'day': new Date(year, month - 1, i).getDay(),
- 'current': true,
- })
- if (i == date) {
- this.setData({
- selectedDay: calendarDays[index - 1]
- })
- }
- if (month === this.data.defaultMonth && i === this.data.defaultDate) {
- if (first) {
- first = false
- this.setData({
- selectedIndex: index - 1,
- blue: index - 1,
- })
- }
- }
- }
- // 下个月显示的天数及日期
- for (let i = 1; i <= 7 - lastDay; i++) {
- let date = new Date(year, month, i)
- calendarDays.push({
- 'year': date.getFullYear(),
- 'month': date.getMonth() + 1,
- 'date': date.getDate(),
- 'day': date.getDay(),
- 'current': false,
- })
- }
- this.setData({
- calendarDays,
- month
- })
- },
- // 手动选中日期
- clickDate(e) {
- let index = e.currentTarget.dataset.index
- let list = this.data.calendarDays
- if (list[index].current) {
- this.triggerEvent('day', list[index])
- this.setData({
- calendarDays: list,
- selectedIndex: index,
- selectedDay: list[index]
- })
- }
- },
- next() {
- this.getMonthDaysCurrent(new Date(this.data.selectedDay.year, this.data.selectedDay.month, 1))
- },
- last() {
- this.getMonthDaysCurrent(new Date(this.data.selectedDay.year, this.data.selectedDay.month - 2, 1))
- }
- }
- })
|