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);
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson