生成一个月日历(函数封装)

整理了一下,这个日历的思路是这样的

  1. 一个月的总天数
  2. 一个月中第一天是星期几,即可知道前面空几个
data(){
  return {
    objmonth:[]
  }
}
methods:{
makeCalendar(year,month){
  let now = new Date()
  let y = now.getFullYear() 
  let m = now.getMonth() 
  let d = now.getDate()
  let now1 = new Date(y,m,d) //当天时间
  let week = now.getDay()
  let endDay = new Date(year,month,0)//下个月的第0天,也说是上个月的最后一天,实例化
  let lastDay = endDay.getDate() //*这个月总天数
  let weekobj = new Date(year,month-1,1) //第一天实例化
  let firstWeek = weekobj.getDay() //第1天周几 即可知道前面空几个
  for(let i = 1;i<=lastDay;i++){
                    let now2 = new Date(year,month,i) //每一天都实例化
                    let _y = _now.getFullYear()
                    let _m = _now.getMonth()
                    let _d = _now.getDate()
                    let _week = _now.getDay()
                    let objday = {
                        ymd: `${_y}-${this.transeNum(_m)}-${this.transeNum(_d)}`
                        day: _d,
                        week: _week,
                        isWeekDay: _week==0 || _week==6,
                        isToday: now1.getTime() == now2.getTime(),
                        isPass: now1.getTime() > now2.getTime()
                    }
                    this.objmonth.push(objday)
                }
                for(let i = 0;i
image.png

你可能感兴趣的:(生成一个月日历(函数封装))