CFLib.org – Common Function Library Project

FirstXDayOfMonth(day_number, month_number, year)

Last updated March 23, 2005
Download UDF

author

Troy Pullis                                       Troy Pullis

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

 
Rated 0 time(s). Average Rating: 0

Description:
Takes a day number, month number, and year. Returns a date object of the first occurance 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:
Returns a date object.

Example:

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

Parameters:

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

Full UDF Source:

<cfscript>
/**
* Returns a date object of the first occurance of a specified day in the given month and year.
*
* @param day_number      An integer in the range 1 - 7. 1=Sun, 2=Mon, 3=Tue, 4=Wed, 5=Thu, 6=Fri, 7=Sat. (Required)
* @param month_number      Month value. (Required)
* @param year      Year value. (Required)
* @return Returns a date object.
* @author Troy Pullis (tpullis@yahoo.com)
* @version 1, March 23, 2005
*/

function FirstXDayOfMonth(day_number,month_number,year) {
var start_of_month = CreateDate(year,month_number,1); // date object of first day for given month/year
var daydiff = DayOfWeek(start_of_month) - day_number;
var return_date = "";
switch(daydiff) {
case "-1": return_date = DateAdd("d",1,start_of_month); break;
case "6": return_date = DateAdd("d",1,start_of_month); break;
case "-2": return_date = DateAdd("d",2,start_of_month); break;
case "5": return_date = DateAdd("d",2,start_of_month); break;
case "-3": return_date = DateAdd("d",3,start_of_month); break;
case "4": return_date = DateAdd("d",3,start_of_month); break;
case "-4": return_date = DateAdd("d",4,start_of_month); break;
case "3": return_date = DateAdd("d",4,start_of_month); break;
case "-5": return_date = DateAdd("d",5,start_of_month); break;
case "2": return_date = DateAdd("d",5,start_of_month); break;
case "-6": return_date = DateAdd("d",6,start_of_month); break;
case "1": return_date = DateAdd("d",6,start_of_month); break;
default: return_date = start_of_month; break; // daydiff=0, default to first day in current month
} //end switch
return return_date;
}
</cfscript>

Search CFLib.org


Latest Additions

Shawn Porter Shawn Porter added
DeMoronize
2 hour(s) ago

Chris Carey Chris Carey added
readPropertiesFi...
1 day(s) ago

Randy Johnson Randy Johnson added
lastDayofWeek
3 day(s) ago

Frank Marion Frank Marion added
sitemapPing
7 day(s) ago

Top Rated

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Barney Boisvert indentXml
Rated 5.0, 3 time(s)

Nathan Dintenfass                                 queryColumnsToSt...
Rated 5.0, 3 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson