[Vuejs]-JS Calendar two days short

0👍

daysInYearMonth() is returning the number of days in the month before the one you ask for. When you use day 0 in a date, it means the last day of the month before it. That’s why you get 30 for July, not 31. So to get the last day of the current month, you need to use the next month.

daysInYearMonth(y,m){
  return new Date(y, m+1, 0).getDate()
}

The next problem is that array keys start at 0, but days in months start at 1. So when you do Array(this.daysInYearMonth(this.year,
this.month)).keys()
it returns an array from 0 to 30 rather than 1 to 31. The simplest way to fix this is to write a loop that starts at 1, rather than playing games with arrays.

let lastday = this.daysInYearMonth(this.year, this.month);
this.days = [];
for (let d = 1; d <= lastday; d++) {
    this.days.push(d);
}

0👍

  • First of all:

In return new Date(y, m, 0).getDate()
you should use m + 1, as 0 takes the last day of the previous month (see http://www.w3resource.com/javascript-exercises/javascript-date-exercise-3.php)

  • Secondly:

this.days is zero-based, but days in a month aren’t. So, you count from 0-30 instead of 1-31.

I hope this helps you out. Greets

Leave a comment