日历封装

getManthDate (year, month) {
this.year = parseInt(year)
this.month = parseInt(month)
if (month === 13) {
month = 1
this.month = 1
year += 1
this.year += 1
}
if (month === 0) {
month = 12
this.month = 12
year -= 1
this.year -= 1
}
var tempArray = []
var ret = []
if (!year || !month) {
var today = new Date()
year = today.getFullYear()
month = today.getMonth() + 1
this.year = year
this.month = month
}
var firstDay = new Date(year, month - 1, 1)
var firstWeekDay = firstDay.getDay()
firstWeekDay = firstWeekDay === 0 ? 7 : firstWeekDay
var lastDayofLastMonth = new Date(year, month - 1, 0)
var lastDateOfLastMonth = lastDayofLastMonth.getDate()
var preMonthDayCount = firstWeekDay - 1
var lastDay = new Date(year, month, 0)
console.log('lastDay', lastDay)
var lastDate = lastDay.getDate()
for (let i = 0; i < 6 * 7; i++) {
var date = i + 1 - preMonthDayCount
var isActiveMonth = 1
var showDate = date
var thisMonth = month
if (date <= 0) {
isActiveMonth = 0
thisMonth = month - 1
showDate = lastDateOfLastMonth + date
} else if (date > lastDate) {
isActiveMonth = 2
thisMonth = month + 1
showDate = showDate - lastDate
}
thisMonth = thisMonth === 0 ? 12 : thisMonth
thisMonth = thisMonth === 13 ? 1 : thisMonth
tempArray.push({
isAct: isActiveMonth,
month: thisMonth,
date: date,
showDate: showDate
})
}
for (let j = 0; j < tempArray.length; j += 7) {
ret.push(tempArray.slice(j, j + 7))
}
this.days = ret
if (!this.dateString) {
this.selectedDate = new Date().getDate()
} else {
this.selectedDate = parseInt(this.dateString.split('-')[2])
}
console.log(this.days, 'days')
console.log('selectedDate', this.selectedDate)
console.log('year', this.year)
console.log('month', this.month)
}

猜你喜欢

转载自www.cnblogs.com/langqq/p/9132090.html