CFLib.org – Common Function Library Project

customWeekNumber(date[, weekStart][, returnFormat])

Last updated March 4, 2010

author

Azadi Saryev

Version: 0 | Requires: CF5 | Library: DateLib

Description:
Returns week number of given date based on optional start-of-week day (numeric; in the range of 1 [Sunday] to 7 [Saturday]; defaults to 1). Returns either week number or, if optional third argument was passed (boolean; defaults to false), as week number and year as WW.YYYY

Return Values:
Returns a string (depends on optional argument).

Example:

<cfoutput>
Week number of today's date based on Saturday as first day of week: #customWeekNumber(now(), 7)#<br>
Week number, including year, of today's date based on Monday as first day of week: #customWeekNumber(now(), 2, true)#
</cfoutput>

Parameters:

Name Description Required
date date to determine week number Yes
weekStart day of week start (1-7) No
returnFormat boolean to return week number and year instead of just week number No

Full UDF Source:

/**
 * Returns week number based on [optional] custom start of week day
 * 
 * @param date 	 date to determine week number  (Required)
 * @param weekStart 	 day of week start (1-7) (Optional)
 * @param returnFormat 	 boolean to return week number and year instead of just week number (Optional)
 * @return Returns a string (depends on optional argument). 
 * @author Azadi Saryev (azadi.saryev@gmail.com) 
 * @version 0, March 4, 2010 
 */
function customWeekNumber(date) {
	var firstDayOfWeek = 1;
	var theDate = createdate(year(arguments.date), month(arguments.date), day(arguments.date));
	var firstDayOfYear = createdate(year(theDate), 1, 1);
	var firstCustWeekdayOfYear = "";
	var custWeekNum = 0;
	if (arraylen(arguments) gte 2) firstDayOfWeek = arguments[2];
	if (val(firstDayOfWeek) lt 1 OR val(firstDayOfWeek) gt 7) firstDayOfWeek = 1;
	firstCustWeekdayOfYear = dateadd('d', firstDayOfWeek-dayofweek(firstDayOfYear), firstDayOfYear);
	if (datecompare(theDate, firstCustWeekdayOfYear) lt 0) {
		firstDayOfYear = createdate(year(theDate)-1, 1, 1);
		firstCustWeekdayOfYear = dateadd('d', firstDayOfWeek-dayofweek(firstDayOfYear), firstDayOfYear);
	}
	custWeekNum = ceiling(datediff('d', firstCustWeekdayOfYear, theDate)/7);
	if (NOT custWeekNum) custWeekNum = 1;
	if (arraylen(arguments) gte 3 AND arguments[3]) {
		return numberformat(custWeekNum + year(firstCustWeekdayOfYear)/(1 & repeatstring(0, len(year(firstCustWeekdayOfYear)))), '^.' & repeatstring(0, len(year(firstCustWeekdayOfYear)))); //return weeknumber and year as WW.YYYY 
	} else {
		return custWeekNum; //return weeknumber only
	}
}
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