– Common Function Library Project

firstXDayOfMonth(dayOfWeek, month, year)

Last updated July 6, 2014

Version: 1 | Requires: ColdFusion 9 | Library: DateLib

Rated 2 time(s). Average Rating: 2.5

Takes a day number, month number, and year. Returns a date object of the first occurrence of that day in the given month and year. For example, you want a date object for the first Wednesday in November, 2005. In this case, the function returns the date: 11/2/05.

Return Values:
The date of the first [dayOfWeek] of the specified month/year


view plain print about
Our Nov CFUG meeting is on #DateFormat(FirstXDayOfMonth(4,11,2005),"dddd, mmmm d")#


Name Description Required
dayOfWeek An integer in the range 1 - 7. 1=Sun, 2=Mon, 3=Tue, 4=Wed, 5=Thu, 6=Fri, 7=Sat. Yes
month Month value. Yes
year Year value. Yes

Full UDF Source:

view plain print about
 * 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 ( 
 * @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;
blog comments powered by Disqus


Latest Additions

CF Ninja CF Ninja added
a while ago

Stephen Withington Stephen Withington added
a while ago

Adam Cameron Adam Cameron added
a while ago

Ray Ford Ray Ford added
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 49 time(s)

Barney Boisvert indentXml
Rated 5.0, 12 time(s)

Rachel Lehman deAccent
Rated 5.0, 9 time(s)

Darwan Leonardo Sitepu splitNumber
Rated 5.0, 9 time(s)

Created by Raymond Camden / Design by Justin Johnson