User:Sunmarsh/Sandbox/Orange

Calculate the difference between calendar value x and the start date value (numeral associated with the day of the week the month starts on), and add 1. If the result of this expression is less than one, then the calendar value is part of the previous month. For these calendar values, take the number of days in the previous month and add to them the result of the following expression: calendar value x - the start date value + 1. If the result of the expression in the first sentence is 1 or greater then determine if calendar value x is equal to or greater than the sum of the number of days in the current month and the start date value. If so, then the calendar value is part of the following month. For these calendar values, subtract the sum of the number of days in the current month and the start date value from calendar value x and add 1. For all other values (the calendar values associated with the days of the current month), calculate the difference between calendar value x and the start date value and add 1.

To determine whether or not the calendar should display a given week, find the sum of these variables: the number of days in the current month, the start date value, and six minus the end date value. Whether or not the week is displayed is dependent on whether this value exceeds 28 (for a fifth week), and then 35 (for a sixth week).