CFLib.org – Common Function Library Project

createLocalisedDayOfWeekAsInteger(locale[, iso])

Last updated December 8, 2013

author

Adam Cameron

Version: 1 | Requires: CF10 | Library: CFMLLib

Description:
Improves dayOfWeekAsInteger() to be locale- and ISO-aware. Thanks to Simon Bingham, Duncan Cumming, Matt Bourke and James Moberg for inspiration for this.

Return Values:
A function which performed localised dayOfWeekAsInteger() operations

Example:

include "createLocalisedDayOfWeekAsInteger.cfm";

LSDayOfWeekAsIntegerFR = createLocalisedDayOfWeekAsInteger("fr_fr");

writeOutput("Dimanche is day of week: #LSDayOfWeekAsIntegerFR("dimanche")#");

Parameters:

Name Description Required
locale The locale to use when localising the returned function Yes
iso Whether to consider Sunday day 1 (default in CFML) or 7 (ISO standard) No

Full UDF Source:

/**
 * Returns a localised version of dayOfWeekAsInteger() (http://www.cflib.org/udf/dayOfWeekAsInteger)
 * v1.0 by Adam Cameron
 * 
 * @param locale 	 The locale to use when localising the returned function (Required)
 * @param iso 	 Whether to consider Sunday day 1 (default in CFML) or 7 (ISO standard) (Optional)
 * @return A function which performed localised dayOfWeekAsInteger() operations 
 * @author Adam Cameron (dac.cfml@gmail.com) 
 * @version 1.0, December 8, 2013 
 */
function function createLocalisedDayOfWeekAsInteger(required string locale, boolean iso=false){
	var supportedLocales = SERVER.coldfusion.supportedLocales;
	if (!listFindNoCase(supportedLocales, locale)){
		throw(type="InvalidLocaleException", message="Invalid locale value specified", detail="Locale must be one of #supportedLocales#");
	}
	var baseDate = createDate(1972, 1, iso ? 31:30); // ie: in ISO mode, start on Mon. Otherwise CF mode: Sun
	var days = "";
	for (var i=0; i < 7; i++){
		days = listAppend(days, lsDateFormat(dateAdd("d", i, baseDate), "dddd", locale));
	}

	return function(required string day){
		var index = listFindNoCase(days, day);
		if (index){
			return index;
		}
		throw(type="ArgumentOutOfRangeException", message="Invalid day value", detail="day argument value (#day#) must be one of #days#");
	};
}
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