brospars / simple-calendar

Simple calendar jquery plugin
https://brospars.github.io/simple-calendar
MIT License
49 stars 38 forks source link

fixedStartDay #26

Closed mhore22 closed 4 years ago

mhore22 commented 4 years ago

fixedStartDay: false does not put Sunday as the first day of the week

monsterbrain commented 4 years ago

going through the code, I think fixedStartDay doesn't seem to be in use anywhere ...

monsterbrain commented 4 years ago

if the behavior you want is, to show the first day of week as Sunday ... following code change achieve that ..

  var tbody = $('<tbody></tbody>');
  // modified code
  if (!this.settings.fixedStartDay) {
    // start the week by sunday
    for (var i = 0; i < this.settings.days.length; i++) {
      thead.append($('<td>' + this.settings.days[i].substring(0, 3) + '</td>'));
    }
  } else {
    //Header day in a week ( (1 to 8) % 7 to start the week by monday)
    for (var i = 1; i <= this.settings.days.length; i++) {
      thead.append($('<td>' + this.settings.days[i % 7].substring(0, 3) + '</td>'));
    }
  }

  //first day of the month
  var firstDay = new Date(y, m, 1);
  // modified code
  if (!this.settings.fixedStartDay) {
    //If not sunday set to previous sunday
    while (firstDay.getDay() != 0) {
      firstDay.setDate(firstDay.getDate() - 1);
    }
  } else {
    //If not monday set to previous monday
    while (firstDay.getDay() != 1) {
      firstDay.setDate(firstDay.getDate() - 1);
    }
  }
brospars commented 4 years ago

fixedStartDay was meant to prevent always beginning by january 1st, february 1st, etc when weeks are overlapping two months.

I am adding a way to set the starting day of weeks in the next release coming today (with backward compatibility, using a boolean).

fixedStartDay: 1 // or true (for backward compatibility) : weeks always begin by monday
fixedStartDay: 0 // weeks always begin by sunday
fixedStartDay: null // or undefined : month always begin by 1 whatever day it is.