创建日历
2020-08-03 本文已影响0人
AAA前端
- 由于样式需求,日历需要自定义。看网上日历一般每个月显示6行。所有用一下函数生成一个月显示的所有日期。
- 返回的数组每一项有月、日、周。当前月如果和数组月相同,样式高亮。
var getDate = function (year, month) {
var ret = []
var firstDay = new Date(year, month - 1, 1)
var firstDayWeekDay = firstDay.getDay()
var premonthDayCount = firstDayWeekDay
for (var i = 0; i < 7 * 6; i++) {
var date = i + 1 - premonthDayCount
var showDate = (new Date(year, month - 1, date)).getDate()
var thisMonth = (new Date(year, month - 1, date)).getMonth() + 1
var thisWeek = (new Date(year, month - 1, date)).getDay()
ret.push({
month: thisMonth,
date: showDate,
week: thisWeek
})
}
return ret
}