简体   繁体   中英

Start week from monday JavaScript

I am using the calendar taken from https://github.com/500tech/angular-simple-calendar .

In this calendar the week starts from sunday and is calculated in this function :

  calculateWeeks = function () {
    $scope.weeks = [];
    var week = null;
    var daysInCurrentMonth = new Date($scope.selectedYear, MONTHS.indexOf($scope.selectedMonth) + 1, 0).getDate();
    for (var day = 1; day < daysInCurrentMonth + 1; day += 1) {
      var dayNumber = new Date($scope.selectedYear, MONTHS.indexOf($scope.selectedMonth), day).getDay();
      week = week || [null, null, null, null, null, null, null];
      week[dayNumber] = {
        year: $scope.selectedYear,
        month: MONTHS.indexOf($scope.selectedMonth),
        day: day
      };

      if (allowedDate(week[dayNumber])) {
        if ($scope.events) { bindEvent(week[dayNumber]); }
      } else {
        week[dayNumber].disabled = true;
      }

      if (dayNumber === 6 || day === daysInCurrentMonth) {
        $scope.weeks.push(week);
        week = undefined;
      }
    }
  };

What changes I should do to start week from Monday?

You could change the definition of dayNumber by adding 6 modulo 7:

var dayNumber = (new Date($scope.selectedYear, MONTHS.indexOf($scope.selectedMonth), day)
                .getDay() + 6) % 7;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM