calendar generation
At work I was faced with the task of trying to generate a calendar and adding events to the specific dates that they appeared on. I wanted to do this in such a way that I could generate the current month, for example, without having to go in and change the code from the previous month.
I also didn’t do a lot of research on the web for this kind of thing, instead I just came up with a quick way to find out what a month looks like by just knowing the number of days in the month and the day of the week on which the month starts.
Pseudo Code
totalDays = number of days in the month;
startDay = day of the week the month starts 1-7;
numberOfDaysInFirstWeek = -1 * startDay + 8;
remainingDaysAfterFirstWeek = totalDays - numberOfDaysInFirstWeek;
numberOfFullWeeks = floor(remainingDaysAfterFirstWeek / 7);
numberOfDaysInLastWeek = remainingDaysAfterFirstWeek % 7;
After determining these values the task is a little self explainatory. Take the numberOfDaysInFirstWeek and loop that many times to create those days, starting withstartDay. Loop for numberOfFullWeeks to create the middle weeks, than loop for numberOfDaysInLastWeek to finish off the month.
