/** * Returns a date object of the first occurrence of a specified day in the given month and year. * v1.0 by Troy Pullis * v1.1 by Adam Cameron (improved/simplified logic, added error handling) * * @param dayOfWeek An integer in the range 1 - 7. 1=Sun, 2=Mon, 3=Tue, 4=Wed, 5=Thu, 6=Fri, 7=Sat. (Required) * @param month Month value. (Required) * @param year Year value. (Required) * @return The date of the first [dayOfWeek] of the specified month/year * @author Troy Pullis (tpullis@yahoo.com) * @version 1.1, July 6, 2014 */ date function firstXDayOfMonth(required numeric dayOfWeek, required numeric month, required numeric year){ if (dayOfWeek < 1 || dayOfWeek > 7){ throw(type="InvalidDayOfWeekException", message="Invalid day of week value", detail="the dayOfWeek argument must be between 1-7 (inclusive)."); } var firstOfMonth = createDate(year, month,1); var dowOfFirst = dayOfWeek(firstOfMonth); var daysToAdd = (7 - (dowOfFirst - dayOfWeek)) MOD 7; var dow = dateAdd("d", daysToAdd, firstOfMonth); return dow; }