if you have ever thought how to accomplish a simple calendar. you'll konw what I'm talking about
每個月有多少天?
- 1月,3月,5月,7月,8月,10月,12月,都是31天
- 2月平年是28天,閏年是29天
- 其他月份:4月,6月,9月,11月
每個月的第一天是星期幾?
function firstDay(date) {
return new Date(date + '-01').getDay()
}
判斷閏年還是平年
function is_leap(year) {
return year % 400 === 0 || year % 4 === 0 && year % 100 !== 0 ? true : false
}
獲取某一年各月的天數(shù)
function m_days(year) {
return [31,28+is_leap(year),31,30,31,30,31,31,30,31,30,31]
}
獲取某年某月的第一天是星期幾
function firstDay(date) {
return new Date(date + '-01').getDay()
}
獲取每個月的天數(shù)數(shù)組
function generateDays(date) {
var year = date.split('-')[0]
var month = date.split('-')[1] - 1
var arr = []
//根據(jù)某年某月的第一天是星期幾來填充空值
for(let j = 0; j < firstDay(date); j++) {
arr.push({value: '', num: ''})
}
for(let i = 0; i < m_days(year)[month]; i++) {
let value = year + '-' + addZero(month+1) + '-' + addZero(i+1)
arr.push({
num: addZero(i+1),
value: value,
})
}
return arr
}