CFLib.org – Common Function Library Project

FirstXDayOfMonth(day_number, month_number, year)

Last updated March 23, 2005

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

 
Rated 1 time(s). Average Rating: 1.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:

view plain print about
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:

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

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
3 day(s) ago

Will Belden Will Belden added
longTime
9 day(s) ago

James Sleeman James Sleeman added
quickSort
19 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
22 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

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

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson