JSFiddle - React, Tailwind, and code Playground

by fredrik

JavaScript

function CalendarController ($scope) {
    var tmpDate = new Date();

    function daysInMonth(date) {
        return new Date(date.getFullYear(), date.getMonth()  + 1, 0).getDate();
    }

    function getCalendarArray () {
        var i, 
            j,
            preDates = tmpDate.getDay() - 1,
            date = 1,
            calendarArray = [],
            week;
        
        for (i = 0; i < $scope.numberOfWeeksToDisplay; i += 1) {
            week = [];
            for (j = 0; j < $scope.numberOfDays; j += 1) {
                if (preDates) {
                    week.push('p' + preDates);
                    preDates -= 1;
                } else {
                    week.push(date);
                    date += 1;                   
                }
            }
            
            calendarArray.push(week);
        }
        
        return calendarArray;
    }

    tmpDate.setDate(1);        

    $scope.numberOfDays = 7;
    $scope.currentDate = new Date();
    $scope.daysInMonth = daysInMonth(tmpDate);
    $scope.numberOfWeeksToDisplay = Math.ceil(((tmpDate.getDay()) + $scope.daysInMonth) / $scope.numberOfDays);
    //$scope.numberOfDaysToDisplay = $scope.numberOfWeeksToDisplay * 7;
    $scope.calendarArray = getCalendarArray();
    console.log($scope.calendarArray);
}

CalendarController({});