如何在第一个和最后一个日期之间填充日期?

时间:2018-05-07 22:26:24

标签: javascript date vue.js calendar

现在: jsffidle

Vue代码

new Vue({
el: '#calendar',
data() {
return {
  date: new Date(),
  daysOfWeek: ['pn', 'vt', 'sr', 'cht', 'pt', 'sb', 'vs']
 }
},
computed: {
days() {
  const date = this.date;

  let days = []
  let amountDays = this.daysInMonth(this.date.getFullYear(), this.date.getMonth() + 1) // TODO fix 
  for (let i = 1; i <= amountDays; i++) {
    days.push(i)
  }
  return days;
 }
},
methods: {
daysInMonth(year, month) {
  return new Date(year, month, 0).getDate()
  }
 }
})

我想要的是什么:

enter image description here

在月份的第一个和最后一个日期之后,我需要填充单元格,以便周日不会开始。

此外,我想在每个月的最后一天之后的几天填充单元格。

1 个答案:

答案 0 :(得分:1)

您需要先计算当月第一天的星期几。

然后计算出日历面板的第一天。

检查以下演示:

&#13;
&#13;
Vue.config.productionTip = false
new Vue({
  el: '#calendar',
  data() {
    return {
      date: new Date(),
      daysOfWeek: ['pn', 'vt', 'sr', 'cht', 'pt', 'sb', 'vs']
    }
  },
  computed: {
    days() {
      const date = this.date;
      let days = []
      let amountDays = this.daysInMonth(this.date.getFullYear(), this.date.getMonth() + 1) // TODO fix 
      
      // get the first day of current month
      let firstDay = new Date(this.date.getFullYear(), this.date.getMonth(), 1)
      // get the day of week (default Montday is 1)
      let dayOfWeek = firstDay.getDay()
      let addDays = function (current, days) {
      	let temp = new Date(current)
        temp.setDate(current.getDate()+days)
        return temp
      }
      // get first day of the calendar panel
      let targetDate = addDays(firstDay, -dayOfWeek)
      // loop 7*5 at here, you can optimize it based on your real demand.
      for(let i = 0; i < 7*5; i++) {
        let calendarDay = addDays(targetDate, i)
      	days.push([calendarDay, calendarDay.getDate()])
      }
      return days;
    }
  },
  methods: {
    daysInMonth(year, month) {
      return new Date(year, month, 0).getDate()
    }
  }
})
&#13;
body {
  margin: 0;
  padding: 0;
}

* {
  box-sizing: border-box;
}

.calendar {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.calendar__wrapper {
  width: 450px;
  height: 400px;
  padding: 20px;
  background: gray;
}

.calendar__days {
  display: flex;
  padding-left: 10px;
  padding-right: 10px;
  flex-wrap: wrap;
 
}

.calendar__footer {
  display: flex;
  justify-content: center;
}



.calendar__header {
  display: flex;
  justify-content: space-between;
  padding-left: 10px;
  padding-right: 10px;
  background-color: rgba(255, 255, 255, 0.6);
}

.calendar__day {
  width: 14.2857%;
  height: 30px;
  color: black;
  font-size: 18px;
}
&#13;
<script src="https://unpkg.com/vue@2.5.16/dist/vue.js"></script>
<div id="calendar">
  <div class="calendar">
    <div class="calendar__wrapper">
      <div class="calendar__header">
        <span v-for="day in daysOfWeek" class="calendar__day">{{ day }}</span>
      </div>
      <div class="calendar__days">
      <p class="calendar__day" v-for="day in days" :title="day[0]">{{ day[1] }}</p>
      </div>
      <footer>
        <div class="calendar__footer">
        <a href="https://stackoverflow.com" target="_blank">Special for answer on StackOverflow</a>
        </div>
      </footer>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;