CFLib.org – Common Function Library Project

getWeekOfMonth(d)

Last updated November 13, 2014

author

Stephen Withington

Version: 0 | Requires: CF10 | Library: DateLib

Description:
Returns the week number in a month for any given date (1-5).

Return Values:
Returns a number.

Example:

<cfscript>
myDate = CreateDate(2014, 6, 27);
weekOfMonth = getWeekOfMonth(myDate);
// returns 4
</cfscript>

Parameters:

Name Description Required
d Date to parse. Yes

Full UDF Source:

/**
 * Returns the week number in a month for any given date (1-5).
 * 
 * @param d 	 Date to parse. (Required)
 * @return Returns a number. 
 * @author Stephen Withington (stephenwithington@gmail.com) 
 * @version 0, November 13, 2014 
 */
numeric function getWeekOfMonth(date d=Now()) {
	var cal = CreateObject('java', 'java.util.GregorianCalendar').init(
		JavaCast('int', Year(arguments.d))
		, JavaCast('int', Month(arguments.d)-1)
		, JavaCast('int', Day(arguments.d))
		, JavaCast('int', Hour(arguments.d))
		, JavaCast('int', Minute(arguments.d))
		, JavaCast('int', Second(arguments.d))
	);
	cal.setMinimalDaysInFirstWeek(1);
	return cal.get(cal.WEEK_OF_MONTH);
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson