CFLib.org – Common Function Library Project

createLocalisedDayOfWeekAsInteger(locale[, iso])

Last updated December 08, 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#");
    };
}

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