CFLib.org – Common Function Library Project

GetNthOccOfDayInMonth(NthOccurrence, TheDayOfWeek, TheMonth, TheYear)

Last updated August 28, 2001

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

 
Rated 3 time(s). Average Rating: 4.0

Description:
Returns the day of the month(1-31) of an Nth Occurrence of a day (1-sunday,2-monday etc.)in a given month. Used to determine holidays and special events that occur on the nth occurrence of a day in a month.

Return Values:
Returns a numeric value.

Example:

view plain print about
<CFOUTPUT>
The third Sunday in August of 2001 is on August
#GetNthOccOfDayInMonth(3,1,8,2001)#.
</CFOUTPUT>

Parameters:

Name Description Required
NthOccurrence A number representing the nth occurrence.1-5. Yes
TheDayOfWeek A number representing the day of the week (1=Sunday, 2=Monday, etc.). Yes
TheMonth A number representing the Month (1=January, 2=February, etc.). Yes
TheYear The year. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * Returns the day of the month(1-31) of an Nth Occurrence of a day (1-sunday,2-monday etc.)in a given month.
 * 
 * @param NthOccurrence      A number representing the nth occurrence.1-5. 
 * @param TheDayOfWeek      A number representing the day of the week (1=Sunday, 2=Monday, etc.). 
 * @param TheMonth      A number representing the Month (1=January, 2=February, etc.). 
 * @param TheYear      The year. 
 * @return Returns a numeric value. 
 * @author Ken McCafferty (mccjdk@yahoo.com) 
 * @version 1, August 28, 2001 
 */

function GetNthOccOfDayInMonth(NthOccurrence,TheDayOfWeek,TheMonth,TheYear)
{
  Var TheDayInMonth=0;
  if(TheDayOfWeek lt DayOfWeek(CreateDate(TheYear,TheMonth,1))){
    TheDayInMonth= 1 + NthOccurrence*7  + (TheDayOfWeek - DayOfWeek(CreateDate(TheYear,TheMonth,1))) MOD 7;
  }
  else{
    TheDayInMonth= 1 + (NthOccurrence-1)*7  + (TheDayOfWeek - DayOfWeek(CreateDate(TheYear,TheMonth,1))) MOD 7;
  }
  //If the result is greater than days in month or less than 1, return -1
  if(TheDayInMonth gt DaysInMonth(CreateDate(TheYear,TheMonth,1)) OR   TheDayInMonth lt 1){
    return -1;
  }
  else{
    return TheDayInMonth;
  }
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Adam Cameron Adam Cameron added
savecontent
17 day(s) ago

Pete Ruckelshaus Pete Ruckelshaus added
firstDayOfWeek
a while ago

Chris Weller Chris Weller added
convertQueryStri...
a while ago

Dan Bracuk Dan Bracuk added
getExcelPercenti...
a while ago

Top Rated

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

Barney Boisvert indentXml
Rated 5.0, 12 time(s)

Rachel Lehman deAccent
Rated 5.0, 8 time(s)

Markus Schneebeli                                 ListRemoveByStri...
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson